0

Hi I have following code snippet which is working, when there is no namespace available but if i make it available then its return no values.

xml file : Person.xml

<?xml version="1.0" encoding="utf-8" ?>
    <Persons>
      <Person name="John Smith">
        <Age>30</Age>
        <Gender>Male</Gender>
      </Person>
      <Person name="Mike Folley">
        <Age>25</Age>
        <Gender>Male</Gender>
      </Person>
      <Person name="Lisa Carter">
        <Age>22</Age>
        <Gender>Female</Gender>
      </Person>
    </Persons>

Loading above xml file to xml document

 string  xmlstr1 = AppDomain.CurrentDomain.BaseDirectory + "Person.xml";
 XmlDocument doc = new XmlDocument();
 doc.Load(xmlstr1);

 XmlNodeList personNodes =  doc.DocumentElement.SelectNodes("Person");

if i add namespace for the document and try to get values using xath it wont work.

Adding name to root

  <?xml version="1.0" encoding="utf-8" ?>
        <Persons xmlns="www.google.com">
          <Person name="John Smith">...
          .....

Then i am trying the get value using following code but no result.

XmlNode root = doc.DocumentElement;

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", root.NamespaceURI);

XmlNodeList node = doc.DocumentElement.SelectNodes("Person", nsmgr);
5
  • Why use XPath? Why not just load it with LINQ to XML, and search for it in a more civilized way? :) Commented Aug 8, 2014 at 20:45
  • @JonSkeet no, this is my requirement. Commented Aug 8, 2014 at 20:52
  • I've tried your code, and it gives me a count of 3. Can you give a short but complete program demonstrating the problem? Just using XmlDocument.Load("test.xml") to load the document is going to be simplest. (I'd still thoroughly recommend moving to LINQ to XML as soon as humanly possible. "This is my requirement" isn't really an explanation...) Commented Aug 8, 2014 at 20:53
  • 1
    @JonSkeet I think you must be having a bad day. The number of times I read an SO question saying "I want to do X using Y", and have to fight the temptation to say "it would be much easier using Z"... Generally, we don't know enough about people's overall project requirements to judge whether their technology choices are reasonable or not; and even if they aren't the best choices, it's often not practical to change them at the current stage of development. Commented Aug 9, 2014 at 7:21
  • @MichaelKay: On the other hand, if you don't even suggest anything, they may never find a better path. In a number of cases, someone has asked for an XPath or XmlDocument-based approach, and after I've shown them how simple it is using LINQ to XML they have changed. I was going to give an answer based on XPath as well, but as I can't even reproduce the problem, I can't do so... Commented Aug 9, 2014 at 7:25

2 Answers 2

1

The namespace doesn't have a prefix, which adds a little more confusion.
Create a namespace with a matching URI, and your own prefix, then use the prefix in your query.

NameTable nt = new NameTable();
XmlNamespaceManager nsmgr;
nsmgr = new XmlNamespaceManager(nt);

string strURI = "www.google.com"; // the default namespace, must match your doc.
nsmgr.AddNamespace("g", strURI);  // add your own prefix (anything you want really)

// Query with the "g" prefix, that you just defined.
XmlNode ndNode = doc.SelectSingleNode("//g:Person", nsmgr);
Sign up to request clarification or add additional context in comments.

1 Comment

In your last example change your XPath from "Person" to "//bk:Person", or to "/bk:Persons/bk:Person", you need to add the "bk" namespace you defined.
1
  1. You didn't actually used registered prefix in the XPath you tried

  2. The path used in XPath is not correct. <Person> isn't the root node, it is located within <Person>.

This way will get it work :

XmlNodeList node = doc.DocumentElement
                      .SelectNodes("/bk:Persons/bk:Person", nsmgr);

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.