2

I have the following XML.

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost/gsainis/GsaInisWebService">
    <string>
        <gsafeed>
            <group action="add">
                <record>
                 ......
                 ......
                </record>
            </group>
        </gsafeed>
   </string>
</ArrayOfString>

I am using C# code (.NET 4.0) to parse this XML. I am using the code below to select all <record> nodes in the above XML.

XmlNamespaceManager xmlnsmgr = new XmlNamespaceManager(INISRecordXMLdoc.NameTable);
xmlnsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlnsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
xmlnsmgr.AddNamespace(string.Empty, "http://localhost/gsainis/GsaInisWebService");

foreach (XmlNode node in INISRecordXMLdoc.SelectNodes("//ArrayOfString/string/gsafeed/group/record",xmlnsmgr))
{
    //Do something
}

The problem is the foreach loop is never executed. What should be correct XPath to be used such that I get all the <record> nodes?

1 Answer 1

6

Try this - I've had trouble in the past with having an "empty" XML prefix:

XmlNamespaceManager xmlnsmgr = new XmlNamespaceManager(INISRecordXMLdoc.NameTable);
xmlnsmgr.AddNamespace("ns", "http://localhost/gsainis/GsaInisWebService");

foreach (XmlNode node in INISRecordXMLdoc.SelectNodes("//ns:ArrayOfString/ns:string/ns:gsafeed/ns:group/ns:record", xmlnsmgr))
{
   // Do something
}

Use something other than an empty string - and use that XML namespace prefix in your XPath.

Sign up to request clarification or add additional context in comments.

1 Comment

I read about XmlNamespaceManager two days ago. And now here. Now I just need to use it my self, and it will be natural for me to use in the future :)

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.