Linq To Xml problems using XElement's method Elements(XName)

此生再无相见时 提交于 2019-12-01 06:47:17
marc_s

Pretty easy: there's a XML namespace in play, which you're ignoring:

<data xmlns="http://www.example.com"  
      **************************

You need to add that to your Linq-to-XML queries!

Something like:

XNamespace ns = "http://www.example.com";

and then

XRoot.Elements(ns + "contact") 

and of course, also use the XML namespace when accessing the child elements:

var results = from e in XRoot.Elements("contact") 
              select new Contact(e.Element(ns + "name").Value, 
                                 e.Element(ns + "email").Value, 
                                 "1-1-1", null, null);

That should help. See the MSDN docs on Working with XML Namespaces for more details.

For me I solved it like that because I didn't had a namespace in my XML:

xmldoc.Root.Elements("contact");

I forgot to use the "Root" method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!