0

I have an XML and i am trying to get the node from XmlDocument, but due to some namespace issues, it is not returning me back the node I want.

The following is my XML.

<?xml version="1.0" encoding="iso-8859-1"?>
<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.origoservices.com">
  <content>
    <application>
      <personal_client id="pc1">
        <title>Mr</title>
        <forenames>Test</forenames>
        <surname>SurName</surname>
        <sex>Male</sex>
      </personal_client>
      <personal_client id="pc2">
        <title>Mr</title>
        <forenames>Test</forenames>
        <surname>SurName</surname>
        <sex>Male</sex>
      </personal_client>
      <personal_client id="pc3">
        <title>Mr</title>
        <forenames>Test</forenames>
        <surname>SurName</surname>
        <sex>Male</sex>
      </personal_client>
    </application>
  </content>
</message>

Following is the C# code which I am using to get all the personal_client nodes from the above xml.

XmlDocument XMLDoc = new XmlDocument();
XMLDoc.PreserveWhitespace = true;
XMLDoc.Load("XML Loaded successfully");

XmlNamespaceManager nsmgr = new XmlNamespaceManager(XMLDoc.NameTable);
nsmgr.AddNamespace("origo", "http://www.origoservices.com");
nsmgr.PushScope();

XmlNodeList xnList = XMLDoc.SelectNodes("//origo:message/m_content/application/personal_client", nsmgr);

All the time it is returning 0 nodes, please help.

3
  • I recommend that you use the newer XML object, so XDocument etc. Commented Nov 7, 2019 at 14:39
  • XMLDoc.Load() expects the path to the xml file as argument, why are you passing a message instead? Commented Nov 7, 2019 at 14:40
  • This way of querying objects in XML is probably the most cumbersome and exasperating approaches. It's quite off putting imo. Commented Mar 3, 2023 at 3:12

2 Answers 2

1

You need to specify the namespace for each element of the query because the default namespace is also applied to the sub-nodes. I've put your sample XML in a file called XmlFile1.xml and created the following sample:

var XMLDoc = new XmlDocument();
XMLDoc.Load("XmlFile1.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(XMLDoc.NameTable);
nsmgr.AddNamespace("origo", "http://www.origoservices.com");
XmlNodeList xnList = XMLDoc.SelectNodes("//origo:message/origo:content/origo:application/origo:personal_client", nsmgr);

This code retrieves 3 nodes.

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

2 Comments

yeah i got the node as well, but could you please tell me why it is not working when i am specifying the origo on the root element.
@Rober namespace declarations are also valid for the child elements. So the default namespace (xmlns=) is also valid on the children. Hence you need to specify it in the XPath query also in order to select the child nodes.
1

Here's another way

//Using the file path
var clients = XDocument.Load(xmlFilePath)
               .Descendants(XName.Get("personal_client", "http://www.origoservices.com"));

//Using the file content
var clients = XDocument.Parse(xmlFileContent)
               .Descendants(XName.Get("personal_client", "http://www.origoservices.com"));

And this way you can avoid putting the namespace

.Descendants().Where(x => x.Name.LocalName == "personal_client");

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.