2

Possible Duplicate:
Use Linq to Xml with Xml namespaces

I have an xml:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">
<Global>
   <Items>
      <Item text="Main text" key="1">
         <Item text="Some text" key="1.1"/>
         <Item text="Other text" key="1.2"/>
         <Item text="Text" key="1.3"/>
      </Item>
      <Item text="Main text 2" key="2">
         <Item text="Some text" key="2.1"/>
         <Item text="Other text" key="2.2"/>
         <Item text="Text" key="2.3"/>
      </Item>
   </Items>
</Global>
</string>

I need to get items inside Item (like with keys 2.1,1.1 etc).

I have this code:

var xml = GetXmlFromWeb(service.ServiceLink, true);
                var stringXml = new StringReader(xml); // Load data from web)
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreWhitespace = true;
                var reader = XmlTextReader.Create(stringXml, settings);

                XDocument docWithDecode = XDocument.Load(reader);
                IEnumerable<XElement> elements = docWithDecode.Root.Descendants("Item");

As result i got Count = 0.

Thats interesting, if i write IEnumerable<XElement> elements = docWithDecode.Root.Descendants(); i got the elemets, but the sructure looks like mess, data was dublicated, each item has namespace refference etc. So something is wrong here.

Can somebody help me to write the working code? Thanks!

Community
  • 1
  • 1
Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45

3 Answers3

1
XNamespace ns = "http://tempuri.org/";
IEnumerable<XElement> elements = docWithDecode.Root.Descendants(ns+"Item");
L.B
  • 114,136
  • 19
  • 178
  • 224
1

As I mentioned in the comments and L.B. mention in his answer, your .Count() == 0 problem is due to the lack of a namespace. See this answer that I linked above to explain it in more details, but effectively your problem is you are searching for an element call "Item" but in fact what you are actually trying to find is namespace + "Item". You just need to add the namespace to the query.

After re-reading your question, I just noticed a problem with your XML and your query. You have an node with several child nodes with the same name. If you use Descendants(), you are going to select all 8, rather than the 6 you specified in your question.

To only select the child <Item> Nodes, you need to modify your query since Descendants() will select anything that matches. You actually need to make use of the .Elements() extension method and select each level specifically.

XNamespace ns = "http://tempuri.org/";
IEnumerable<XElement> elements = docWithDecode.Root
                                              .Elements(ns + "Global")
                                              .Elements(ns + "Items")
                                              .Elements(ns + "Item")
                                              .Elements(ns + "Item");

You can also use Descendants(ns + "Items") instead of the first 2 Elements() in the query since it doesn't repeat inside itself.

Community
  • 1
  • 1
psubsee2003
  • 8,563
  • 8
  • 61
  • 79
0

First thing, don't use Root.Descendants(), this will return an IEnumerable<XElement> of all the children and won't keep the tree structure. It treats all the XElement children the same, no matter what is their location in the tree. You should always try using Root.Elements(ns + "InnerElementName");, this will also return an IEnumerable<XElement>, but only for the children of the Root, (not for the grandchildren :)).

The reason you received 0 elements using Descendants("Item") is because the namespace was missing. Make sure you add the namespace, if there is a namespace in the XML, in my experience, using the element name won't work without adding it.

This code will traverse the XML file in the structure you gave:

XNamespace ns = docWithDecode.Root.Name.Namespace;
XElement xGlobal = docWithDecode.Root.Element(ns + "Global");
forach (XElement xItems in xGlobal.Elements(ns + "Items")
{
   XAttribute outerItemText = xItems.Attribute("text");
   XAttribute outerItemKey = xItems.Attribute("key");

   foreach (XElement xItem in xItems.Elements(ns + "Item"))
   {
      XAttribute innerItemText = xItem.Attribute("text");
      XAttribute innerItemKey = xItem.Attribute("key");
   }
}

I didn't debug this (I don't have VS in this computer) so tell me if it doesn't work, but I think you'll get the idea.

Shahar
  • 655
  • 2
  • 7
  • 23