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!