使用 XElement 解析 xml 时无法找到根节点 - Windows Phone 7
我已经开始在 WP7 中解析 XML 文件,直到现在才发现它非常简单。我当前的 XML 是这样的:
<Node1 attrib1="abc" attrib2="def">
<Node2>
<Node3>
</Node3>
<Node2>
<Node1>
如您所见,我的根节点本身有一些属性,我打算访问它们,但使用这段代码我无法做到这一点:
streamResult = myXMLState.AsyncXMLResponse.GetResponseStream();
XElement myXml = XElement.Load(streamResult);
var parse = from feed in myXml.Descendants("Node1")
select new MyCustomDataType
{
Attribute1 = feed.Attribute("attrib1").Value,
Attribute2 = feed.Attribute("attrib2").Value,
};
变量“parse”的大小在这里总是返回 0 。我做错了什么吗?一如既往地感谢你的帮助!
I have moved into parsing XML files in WP7 and until now was finding it quite straightforward. My current XML is something like this :
<Node1 attrib1="abc" attrib2="def">
<Node2>
<Node3>
</Node3>
<Node2>
<Node1>
As you can see, my root node itself has some attributes and I intend to access them, but with this code I am NOT able to do that :
streamResult = myXMLState.AsyncXMLResponse.GetResponseStream();
XElement myXml = XElement.Load(streamResult);
var parse = from feed in myXml.Descendants("Node1")
select new MyCustomDataType
{
Attribute1 = feed.Attribute("attrib1").Value,
Attribute2 = feed.Attribute("attrib2").Value,
};
The size of variable "parse" always returns 0 here. Am I doing something wrong. Appreciate ur help, as always!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要求的是元素的后代 - 其中不包括元素本身。
或者将其作为文档加载(其中根节点将被视为文档的后代)或使用
DescendantsAndSelf
。You're asking for the descendants of the element - which won't include the element itself.
Either load it as a document instead (where the root node will count as a descendant of the document) or use
DescendantsAndSelf
.