解析多个子标签后,IEnumerable 引用未设置为对象?
我有一些 XML:
<messages>
<message>
<meta appid="1112" date="20111028" msgid="4498016" msgno="855" permlinkdate="2011-10-28">
<summary>RedBook is a fun book</summary>
</meta>
<ISBN>1234123412123</ISBN>
</message>
<message>
<meta appid="1112" date="20111028" msgid="4498016" msgno="855" permlinkdate="2007-1-30">
<summary>BlueBook is a good book</summary>
</meta>
<ISBN>123412341234</ISBN>
</message>
</messages>
我有一些 Linq to XML 来操作此 XML:
public static List<ReviewDTC> GetReviews()
{
XElement xmlDoc = XElement.Load(@"C:\Users\inelson\Desktop\fiddle.xml");
var dtos = from item in xmlDoc.Descendants("message")
select new ReviewDTC()
{
PubDate = item.Element("meta").Attribute("permlinkdate").Value,
Summary = item.Element("summary").Value,
Isbn = item.Element("ISBN").Value
};
List<ReviewDTC> reviews = new List<ReviewDTC>();
reviews = dtos.ToList();
return reviews;
}
上面代码的目的是解析每个消息节点的文档,并为每个消息节点创建一个包含该节点的子元素的对象(以及一个属性)。每个对象都被添加到一个集合中,依此类推。
调试此代码时,我在 var dtos = 之后放置一个 BP,它断言在实例化新的 ReviewDTC 对象时,dtos 没有被设置为对象的实例。解析器是否无法读取 Meta、Summary 或 ISBN 来构建对象?这是怎么回事?
我很感激,很高兴。
I have some XML:
<messages>
<message>
<meta appid="1112" date="20111028" msgid="4498016" msgno="855" permlinkdate="2011-10-28">
<summary>RedBook is a fun book</summary>
</meta>
<ISBN>1234123412123</ISBN>
</message>
<message>
<meta appid="1112" date="20111028" msgid="4498016" msgno="855" permlinkdate="2007-1-30">
<summary>BlueBook is a good book</summary>
</meta>
<ISBN>123412341234</ISBN>
</message>
</messages>
And I have some Linq to XML to operate on this XML:
public static List<ReviewDTC> GetReviews()
{
XElement xmlDoc = XElement.Load(@"C:\Users\inelson\Desktop\fiddle.xml");
var dtos = from item in xmlDoc.Descendants("message")
select new ReviewDTC()
{
PubDate = item.Element("meta").Attribute("permlinkdate").Value,
Summary = item.Element("summary").Value,
Isbn = item.Element("ISBN").Value
};
List<ReviewDTC> reviews = new List<ReviewDTC>();
reviews = dtos.ToList();
return reviews;
}
The purpose of the above code is to parse the document for each message node, and for each message node, create an object with that node's child elements (and one attribute). Each object is added to a collection and so on..
When debugging this code, I place a BP after var dtos = and it is asserting that dtos is not being set to an instance of an object when it is instantiating a new ReviewDTC object. Is the parser not able to read from Meta, Summary or ISBN to build the object? Whats going on?
I appreciate it, big time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
summary
元素位于meta
元素下。试试这个:
The
summary
element is under themeta
element.Try this: