2

I have been unable to parse an Xml which contains multiple namespaces as below:

    <?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header>
      <h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" MajorVersion="14" MinorVersion="3" MajorBuildNumber="352" MinorBuildNumber="0" Version="Exchange2010_SP2" />
   </s:Header>
   <s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <GetMailTipsResponse xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" ResponseClass="Success">
         <ResponseCode>NoError</ResponseCode>
         <ResponseMessages>
            <MailTipsResponseMessageType ResponseClass="Success">
               <ResponseCode>NoError</ResponseCode>
               <m:MailTips xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
                  <t:RecipientAddress xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
                     <t:Name />
                     <t:EmailAddress>[email protected]</t:EmailAddress>
                     <t:RoutingType>SMTP</t:RoutingType>
                  </t:RecipientAddress>
                  <t:PendingMailTips xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" />
                  <t:OutOfOffice xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
                     <t:ReplyBody>
                        <t:Message />
                     </t:ReplyBody>
                  </t:OutOfOffice>
               </m:MailTips>
            </MailTipsResponseMessageType>
         </ResponseMessages>
      </GetMailTipsResponse>
   </s:Body>
</s:Envelope>

I tried the following code but as you can see the first node with Soap Name Space works fine but thereafter I am unable to retrieve the node information that I need which is the node -

/s:Envelope/s:Body/GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType/m:MailTips/t:RecipientAddress/t:Message

Here is the code that I tried:

string getXmlInfo (string resultXml) 

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resultXml);

XmlNamespaceManager soapNsManager = new XmlNamespaceManager(xmlDoc.NameTable);
soapNsManager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
XmlNode xmlNode = xmlDoc.SelectSingleNode("//s:Envelope/s:Body", soapNsManager); //works fine - the node now contains the Xml starting with the node 

XmlNode xmlNode1 = xmlDoc.SelectSingleNode("//s:Envelope/s:Body/GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType", soapNsManager); //returns NULL


XmlNode innerNode1 = xmlNode.SelectSingleNode("//GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType"); // returns NULL
XmlNode innerNode2 = xmlNode.SelectSingleNode("//GetMailTipsResponse", soapNsManager); //returns NULL

// the next line throws an exception
//XmlNode messageNode = xmlDoc.SelectSingleNode("/s:Envelope/s:Body/GetMailTipsResponse/ResponseMessages/MailTipsResponseMessageType/m:MailTips/t:RecipientAddress/t:Message", manager); 

}

Here is what I tried based on response from @LocEngineer:

            XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
        manager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
        manager.AddNamespace("blank", "http://schemas.microsoft.com/exchange/services/2006/types");
        manager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/types");
        manager.AddNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");

        XmlNode messageNode = xmlDoc.SelectSingleNode("/s:Envelope/s:Body/blank:GetMailTipsResponse/blank:ResponseMessages/blank:MailTipsResponseMessageType/m:MailTips/t:OutOfOffice/t:ReplyBody/t:Message", manager);

The messageNode shows up as NULL

1 Answer 1

1

You also need an XmlNamespaceManager prefix for the prefix-less namespace introduced in GetMailTipsResponse node, which contains the nodes without prefix in XML:

GetMailTipsResponse xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"

And then use that in your XPath appropriately, using "m" as prefix for the prefix-less nodes:

XmlNamespaceManager soapNsManager = new XmlNamespaceManager(xmlDoc.NameTable);
soapNsManager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
soapNsManager.AddNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
XmlNode xmlNode1 = xmlDoc.SelectSingleNode("/s:Envelope/s:Body/m:GetMailTipsResponse/m:ResponseMessages/m:MailTipsResponseMessageType", soapNsManager);
Sign up to request clarification or add additional context in comments.

5 Comments

@codelearner Sorry, I overlook that GetMailTipsResponse introduces another namespace without prefix. Adapted the code above.
I have tried that earlier too and did not seem to work.
@codelearner Could you please give a little more detail than just 'did not seem to work'? Works for me! Please update your code to reflect what you try, what exactly doesn't work and what exactly you expect at which place.
My apologies for not being explicit. Have revised the code above. (Looks like it is not easy to insert code in the comment here - reached the comment text size limit).
I see. Please take a hard look at your code. The namespace for "m" is wrong. Compare to mine, the url says "messages" at the end, not "types". ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.