views:

450

answers:

1

Hello everyone. I have a problem using Linq To Xml.

A simple code. I have this XML:

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

I want to get every contact mapping it on an object Contact. To do this I use this fragment 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);

Ok just don't mind that all: focus on this:

When I get the root node, it is all right, I get it correctly.

When I use the select directive I try to get every node saying: from e in

XRoot.Elements("contact")

OK here's the problem: if I use: from e in XRoot.Elements() I get all contact nodes, but if I use: from e in XRoot.Elements("contact") I GET NOTHING: Empty SET.

OK you tell me: Use the other one: OK I DO SO, let's use: from e in XRoot.Elements(), I get all nodes anyway, THAT's RIGHT BUT HERE COMES THE OTHER PROBLEM: When Saying: select new Contact((string)e.Element("name"), (string)e.Element("email"), "1-1-1", null, null); I Try to access <name>, <email>... I HAVE TO USE .Element("name") AND IT DOES NOT WORK TOO!!!!!!!!WHAT THE HELL IS THIS????????????? IT SEEMS THAT I DOES NOT MATCH THE NAME I PASS But how is it possible. I know that Elements() function takes, overloaded, one argument that is an XName which is mapped onto a string. Please consider that the code I wrote come from an example, It should work. Please somebody help me.

THANKS IN ADVANCE

+1  A: 

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

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

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

Something like:

XNamespace ns = "http://www.xxx.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.

marc_s
This is probably my only complaint with linq2xml API. Most software I've worked on isn't interested in the namespace, but code is needed to handle it in any case.
Frank Schwieterman
@Frank: but what good is it to have namespaces (designed specifically to disambiguate elements of the same name) when your API blatantly ignores them?? Doesn't make a whole lot of sense to me, really....
marc_s
REALLY... I TRY IT NOW, Anyway, Thanks thanks thanks, it's incredible, you answered me so fastly.... Thanks for real, i'll let you know how it is going... Just have to try :)
Dorian McHensie
OK IT WORKS... THANK YOU VERY MUCH. I didn0t really focused on namespaces :) Thank you very much!
Dorian McHensie
@marc_s namespaces are useful in XML but rarely do I need to read from different namespaces. The need to respecify the namespace constantly seems like overkill.
Frank Schwieterman