views:

249

answers:

1

Hi,

How to extract method name and namespace from this xml using LINQ to XML?

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;&lt;soap:Header&gt;&lt;wsa:Action&gt;http://www.webserviceX.NET/GetISOCountryCodeByCountyName&lt;/wsa:Action&gt;&lt;wsa:MessageID&gt;urn:uuid:047f0012-b7d2-408d-bdd7-aa556edf70e8&lt;/wsa:MessageID&gt;&lt;wsa:ReplyTo&gt;&lt;wsa:Address&gt;http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous&lt;/wsa:Address&gt;&lt;/wsa:ReplyTo&gt;&lt;wsa:To&gt;   </wsa:To><wsse:Security><wsu:Timestamp wsu:Id="Timestamp-2a4072a3-338c-434a-90ff-5f7d0aa6acbc"><wsu:Created>2009-10-22T19:20:22Z</wsu:Created><wsu:Expires>2009-10-22T19:25:22Z</wsu:Expires></wsu:Timestamp></wsse:Security></soap:Header><soap:Body><GetISOCountryCodeByCountyName xmlns="http://www.webserviceX.NET"&gt;&lt;CountryName&gt;India&lt;/CountryName&gt;&lt;/GetISOCountryCodeByCountyName&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;

I need to get the value GetISOCountryCodeByCountyName and http://www.webserviceX.NET

+2  A: 
public static void ReadXmlUsingLinq()
{
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"&gt;&lt;soap:Header&gt;&lt;wsa:Action&gt;http://www.webserviceX.NET/GetISOCountryCodeByCountyName&lt;/wsa:Action&gt;&lt;wsa:MessageID&gt;urn:uuid:047f0012-b7d2-408d-bdd7-aa556edf70e8&lt;/wsa:MessageID&gt;&lt;wsa:ReplyTo&gt;&lt;wsa:Address&gt;http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous&lt;/wsa:Address&gt;&lt;/wsa:ReplyTo&gt;&lt;wsa:To&gt;   </wsa:To><wsse:Security><wsu:Timestamp wsu:Id=\"Timestamp-2a4072a3-338c-434a-90ff-5f7d0aa6acbc\"><wsu:Created>2009-10-22T19:20:22Z</wsu:Created><wsu:Expires>2009-10-22T19:25:22Z</wsu:Expires></wsu:Timestamp></wsse:Security></soap:Header><soap:Body><GetISOCountryCodeByCountyName xmlns=\"http://www.webserviceX.NET\"&gt;&lt;CountryName&gt;India&lt;/CountryName&gt;&lt;/GetISOCountryCodeByCountyName&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;");

            var query = from XmlNode a in xdoc.DocumentElement where a.Name == "soap:Body" select a.FirstChild;
            foreach (XmlNode node in query)
            {
                Console.WriteLine(node.Name);
                Console.WriteLine(node.Attributes["xmlns"].Value);
            }
            Console.ReadLine();
}
Chris Ballance