Linq To Xml Problems Using the XElement Method Elements (XName)

I have a problem with Linq To Xml.

Simple code. I have this XML:

<?xml version="1.0" encoding="utf-8" ?> <data xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/directory file.xsd"> <contact> <name>aaa</name> <email> email@email.ext </email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description>Description for this contact</description> </contact> <contact> <name>sss</name> <email> email@email.ext </email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description>Description for this contact</description> </contact> <contact> <name>bbb</name> <email> email@email.ext </email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description>Description for this contact</description> </contact> <contact> <name>ccc</name> <email> email@email.ext </email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description>Description for this contact</description> </contact> 

I want each contact to match it by Contact object. For this I use this piece of code:

 XDocument XDoc = XDocument.Load(System.Web.HttpRuntime.AppDomainAppPath + this.filesource); XElement XRoot = XDoc.Root; //XElement XEl = XElement.Load(this.filesource); var results = from e in XRoot.Elements("contact") select new Contact((string)e.Element("name"), (string)e.Element("email"), "1-1-1", null, null); List<Contact> cntcts = new List<Contact>(); foreach (Contact cntct in results) { cntcts.Add(cntct); } Contact[] c = cntcts.ToArray(); // Encapsulating element Elements<Contact> final = new Elements<Contact>(c); 

Well, just don't mind everything: focus on this:

When I get the root node, everything is fine, I understood correctly.

When I use the select directive, I try to get each node text: from e in

 XRoot.Elements("contact") 

In this case, the problem is: if I use: from e to XRoot.Elements () I get all the contact nodes, but if I use: from e to XRoot.Elements ("contact"), I have NOTHING: Empty SET.

OK, you tell me: use another: OK, I do so, let use: from e in XRoot.Elements() , I still get all the nodes, WHAT IS CORRECT, BUT ANOTHER PROBLEM HERE COMES HERE: When you say: select new Contact((string)e.Element("name"), (string)e.Element("email"), "1-1-1", null, null); I am trying to access <name>, <email> ... I am using .Element ("name") AND THIS DOES NOT WORK TOO TOO !!!!!!!! WHAT IS THIS IT ????????????? IT'S WATCHING THAT I DO NOT GATHER THE NAME I AM PASSING But how is this possible. I know that the Elements () function accepts, overloads one argument, which is the XName that maps to the string. Please note that the code I wrote is from an example, it should work.

+4
source share
2 answers

Pretty easy: there is an XML namespace in the game that you ignore:

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

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

Sort of:

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

and then

 XRoot.Elements(ns + "contact") 

and, of course, also use the XML namespace when referring to 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); 

This should help. For more information, see the MSDN docs on Working with XML Namespaces .

+16
source

For me, I solved it like this because I did not have a namespace in my XML:

 xmldoc.Root.Elements("contact"); 

I forgot to use the "Root" method.

0
source

Source: https://habr.com/ru/post/1308611/


All Articles