0

I am having trouble parsing the following XML in C#, because of the "xmlns" attribute in the root.

<CaptureResponse xmlns="http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01">
    <CaptureResult>
        <State>Open</State>
    </CaptureResult>
</CaptureResponse>

How would I, for example, check for the existence of the element in C# with the above xml? The below does not work.

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(response);
        if (doc.SelectSingleNode("CaptureResponse") != null) 
        {}

2 Answers 2

1

An XmlNamespaceManager is needed. You create one and add a reference to the namespace in your XML document.

The "s" in the AddNamespace() is an example of a Namespace prefix that can be used in your selection queries instead of having to prefix every node with the full Namespace. S can be replaced with whatever prefix you wish to use.

XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("s", "http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01");
XmlNode myNode = doc.SelectSingleNode("s:CaptureResponse", nsMgr);
Sign up to request clarification or add additional context in comments.

Comments

0

Your XPath is ambiguous. Try this:

XmlNode Root = doc.SelectSingleNode("/CaptureResponse");

Comments

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.