0

I am trying to read some data from xml. below is my xml structure.

 <Configuration> 
<node1></node1> 
<unity
 xmlns=xmlns="http://schemas.microsoft.com/practices/2010/unity">
 <namesapce name="somename"/> . . . 
</unity> 
</Configuration>

I have tried to read the node "unity" using below xpath

 var nodelist=doc.SelectNodes("/configuration/unity[@xmlns='http://schemas.microsoft.com/practices/2010/unity']");

But it returns null. Whats wrong with my code?

user3610920
  • 1,582
  • 2
  • 13
  • 24
  • Well your XML is broken, you have `xmlns=xmlns="..."` when there should be just one `xmlns="..."`. Assuming that's a typo in the question, the `xmlns="http://schemas.microsoft.com/practices/2010/unity"` is an [XML namespace declaration](https://en.wikipedia.org/wiki/XML_namespace#Namespace_declaration) so you need to follow the instructions in [How do I select nodes that use a default namespace?](https://stackoverflow.com/q/14370989) and [Using Xpath With Default Namespace in C#](https://stackoverflow.com/q/585812). – dbc Oct 30 '17 at 07:49

1 Answers1

0

Try this: (Ignoring namespace)

    string xmlString = "<Configuration><node1></node1><unity xmlns=\"http://schemas.microsoft.com/practices/2010/unity\"><namesapce name=\"somename\"/></unity></Configuration>";
    XmlDocument xd = new XmlDocument();
    xd.LoadXml(xmlString);
    var nodes = xd.SelectNodes("//*[local-name()='unity']");

    Console.WriteLine(nodes.Count);
Sunil
  • 3,404
  • 10
  • 23
  • 31