使用 Linq 抓取 XML 时获取错误信息
图片 我有这个 XML:
<ipb>
<profile>
<id>335389</id>
<name>stapia.gutierrez</name>
<rating>0</rating>
</profile>
</ipb>
我正在尝试获取 ID、姓名和评级。有什么指导吗?
这是我所拥有的和我收到的:
public User FindInformation()
{
string xml = new WebClient().DownloadString(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}", userID));
XDocument doc = XDocument.Parse(xml);
var id = from u in doc.Descendants("profile")
select (string)u.Element("id");
var name = from u in doc.Descendants("profile")
select (string)u.Element("name");
var rating = from u in doc.Descendants("profile")
select (string)u.Element("rating");
User user = new User();
user.ID = id.ToString();
user.Name = name.ToString();
user.Rating = rating.ToString();
return user;
}
这是我在 TextBox 中得到的用于测试目的的内容。
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]
Image I have this XML:
<ipb>
<profile>
<id>335389</id>
<name>stapia.gutierrez</name>
<rating>0</rating>
</profile>
</ipb>
I'm trying to get ID, Name and Rating. Any guidance?
Here's what I have and what I receive:
public User FindInformation()
{
string xml = new WebClient().DownloadString(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}", userID));
XDocument doc = XDocument.Parse(xml);
var id = from u in doc.Descendants("profile")
select (string)u.Element("id");
var name = from u in doc.Descendants("profile")
select (string)u.Element("name");
var rating = from u in doc.Descendants("profile")
select (string)u.Element("rating");
User user = new User();
user.ID = id.ToString();
user.Name = name.ToString();
user.Rating = rating.ToString();
return user;
}
This is what I get in my TextBox for testing purposes.
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要提取
的单个实例,然后对其进行操作:您现在要做的是选择节点列表 (
doc.Descendants("profile")< /code> 将返回一个节点列表,可能只有一个元素 - 但仍然是一个列表),然后是该列表中的所有“id”元素......我猜这不是你想要的!
You need to extract a single instance of
<profile>
and then operate on that:What you're doing now is selecting a list of nodes (
doc.Descendants("profile")
will return a list of nodes, possibly with just one element - but still a list), and then all the "id" elements from within that list.... not really what you want, I guess!这&像这样的其他语句将返回一个可枚举的 &不是具体实例。
即,如果您的 xml 有许多满足条件的节点,会发生什么?
因此,如果您希望获取第一个项目(或者如果您有一个与上面所示完全相同的 xml 结构,没有额外的节点),则应该调用
First
或FirstOrDefault
帮助。This & other statements like these will return you an enumerable & not a specific instance.
i.e what happens, if your xml has many nodes that satisfy the condition?
So, if you are looking to get the first item (or if you have a xml structure exactly as shown above with no extra nodes), a call to
First
orFirstOrDefault
should help.