LinqToXML 为什么这不起作用?
我有我的示例 XML 文件:
<?xml version="1.0" encoding="utf-8" ?>
<contacts>
<contact contactId="2">
<firstName>Barney</firstName>
<lastName>Gottshall</lastName>
</contact>
<contact contactId="3">
<firstName>Armando</firstName>
<lastName>Valdes</lastName>
</contact>
<contact contactId="4">
<firstName>Adam</firstName>
<lastName>Gauwain</lastName>
</contact>
</contacts>
和我的程序:
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
public class Program
{
public class contact
{
public int contactId { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public override string ToString()
{
return firstName+" "+lastName;
}
}
public static void Main()
{
string test;
XDocument xDocument = XDocument.Load("items.xml");
var all = from a in xDocument.Elements("contact")
where a.Attribute("contactId")!=null
select new contact
{
contactId = (int) a.Attribute("contactId"),
firstName = (string) a.Attribute("firstName"),
lastName = (string) a.Attribute("lastName")
};
if (all == null)
Console.WriteLine("Null !");
else
foreach (contact t in all)
{
test = t.ToString();
Console.WriteLine(t.ToString());
}
}
Console.ReadKey();
}
}
此代码向我显示空白控制台窗口。不存在“空”!并且没有接触元件。我花了很多时间思考为什么......有人可以帮助我吗?当我在 foreach 语句中放置断点时,它不起作用
I have my sample XML file:
<?xml version="1.0" encoding="utf-8" ?>
<contacts>
<contact contactId="2">
<firstName>Barney</firstName>
<lastName>Gottshall</lastName>
</contact>
<contact contactId="3">
<firstName>Armando</firstName>
<lastName>Valdes</lastName>
</contact>
<contact contactId="4">
<firstName>Adam</firstName>
<lastName>Gauwain</lastName>
</contact>
</contacts>
and my Program:
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
public class Program
{
public class contact
{
public int contactId { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public override string ToString()
{
return firstName+" "+lastName;
}
}
public static void Main()
{
string test;
XDocument xDocument = XDocument.Load("items.xml");
var all = from a in xDocument.Elements("contact")
where a.Attribute("contactId")!=null
select new contact
{
contactId = (int) a.Attribute("contactId"),
firstName = (string) a.Attribute("firstName"),
lastName = (string) a.Attribute("lastName")
};
if (all == null)
Console.WriteLine("Null !");
else
foreach (contact t in all)
{
test = t.ToString();
Console.WriteLine(t.ToString());
}
}
Console.ReadKey();
}
}
This code shows me blank console window. There's no "Null !" and no contact element. I spent a lot of time thinking why is that... could someone help me? when I put breakpoint inside foreach statement it's not working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
乞讨2024-11-09 10:54:40
尝试使用 Descendants()
而不是 Elements()
var all = from a in xDocument.Descendants("contact")
where a.Attribute("contactId") != null
select new contact
{
contactId = (int) a.Attribute("contactId"),
firstName = (string) a.Attribute("firstName"),
lastName = (string) a.Attribute("lastName")
};
或 Element("contacts").Elements("contact")
var all = from a in xDocument.Element("contacts").Elements("contact")
where a.Attribute("contactId") != null
select new contact
{
contactId = (int) a.Attribute("contactId"),
firstName = (string) a.Attribute("firstName"),
lastName = (string) a.Attribute("lastName")
};
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
实际上有几个原因。
Elements
仅提供文档的顶级元素(“contacts”元素),因此您应该使用后代
来获取较低级别的元素。这是工作代码:
There are actually a couple of reasons.
Elements
only gives you top-level elements of the document (the "contacts" element), so you should useDescendants
to get lower-level elements).Here's working code: