Why XDocument can't get element out of this wellform XML text?

watch

New Member
I'm trying to get the value of the \[code\]Address\[/code\] element from the following XML text, but it's not finding it unless I remove \[code\]xmlns="http://www.foo.com"\[/code\] from the \[code\]Root\[/code\] element. However, the XML is valid even with it. What's the problem here?Since I'm getting the XML text from a web service, I don't have control over it, but I can strip out the \[code\]xmlns\[/code\] part if I have to as the last resort.\[code\]<?xml version="1.0" encoding="utf-8"?><Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.foo.com"> <Address>Main St SW</Address></Root>\[/code\]\[code\]var doc = XDocument.Parse(xmlTextAbove);var address = doc.Descendants().Where(o => o.Name == "Address").FirstOrDefault();Console.WriteLine(address.Value); // <-- error, address is null.\[/code\]
 
Back
Top