LINQ要使用的XElement的方法元素XML问题(的XName) [英] Linq To Xml problems using XElement's method Elements(XName)
问题描述
我使用LINQ to XML中的问题。
I have a problem using Linq To Xml.
一个简单的代码。我有这样的XML:<?/ p>
A 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 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.
当我用select指令我试图让每一个节点曰:在
When I use the select directive I try to get every node saying: from e in
XRoot.Elements("contact")
确定这里有一个问题:如果我使用:从E在XRoot.Elements()我得到的所有接触节点,但如果我使用:从E在XRoot。元素(接触)我得到什么:空集
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,你告诉我:使用另外一个:好吧,我这样做,让我们使用:
从E在XRoot.Elements()
,我得到的所有节点无论如何,这是正确的,但来这里的另一个问题:
时说:选择新的联系人((字符串)e.Element(name)的,(串)e.Element(邮件),1-1-1,NULL,NULL);
我试着访问<名称>,<电子邮件和GT;
...我必须使用.Element(名称),并没有工作过!!!!!! !!这到底是什么?????????????看来,我不匹配的名字我通过,但怎么可能。我知道,元素()函数,重载,这是一个被映射到一个字符串的XName的一个参数。请考虑,我写来自一个例子中的代码,它应该工作
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.
推荐答案
很简单:有一个XML的发挥空间,这你忽略:
Pretty easy: there's a XML namespace in play, which you're ignoring:
<data xmlns="http://www.example.com"
**************************
$ !b $ b
您需要将它添加到您的LINQ到XML查询
You need to add that to your Linq-to-XML queries!
是这样的:
XNamespace ns = "http://www.example.com";
然后
and then
XRoot.Elements(ns + "contact")
当然,也可以使用访问子元素时,XML命名空间:
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);
这应该帮助。查看 MSDN文档与XML命名空间合作一>了解更多信息。
That should help. See the MSDN docs on Working with XML Namespaces for more details.
这篇关于LINQ要使用的XElement的方法元素XML问题(的XName)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!