XML 根元素的值完全错误,未找到子节点
我有一个非常基本的 XML 文件,如下所示:
<allData>
<allDataDetails>
<quoteid>ABC123</quoteid>
<customername>John Smith</customername>
</allDataDetails>
<allDataDetails>
<quoteid>DEF456</quoteid>
<customername>Jane Doe</customername>
</allDataDetails>
</allData>
我的 XSD 指定必须至少存在 1 个 allDataDetails 元素。该文档经过验证良好。
但是,当使用 Linq to XML 进行查询时,我似乎无法识别或查询 allData 中的内部元素。相反,当我在调试器中查看时,Value 属性是所有连接的数据。它看起来像这样:
ABC123John SmithDEF456Jane Doe
这是我的查询代码。 myRows 始终为空,因为我似乎无法获得后代:
XDocument entityXml = XDocument.Parse(myDataString);
var myRows = from d in entityXml.Descendants("allDataDetails")
select new
{
quoteid = d.Element("quoteid").Value,
customername = d.Element("customername").Value
};
有人知道这里可能出了什么问题吗?
I have a very basic XML file that looks like this:
<allData>
<allDataDetails>
<quoteid>ABC123</quoteid>
<customername>John Smith</customername>
</allDataDetails>
<allDataDetails>
<quoteid>DEF456</quoteid>
<customername>Jane Doe</customername>
</allDataDetails>
</allData>
My XSD specifies that at least 1 allDataDetails element must exist. The doc is validated fine.
When querying using Linq to XML, though, I cannot seem to recognize or query for the inner elements within allData. Instead, when I view in the debugger, the Value attribute is all the data concatenated. It looks like this:
ABC123John SmithDEF456Jane Doe
Here's my query code. myRows is always null, because I cannot seem to get the descendants:
XDocument entityXml = XDocument.Parse(myDataString);
var myRows = from d in entityXml.Descendants("allDataDetails")
select new
{
quoteid = d.Element("quoteid").Value,
customername = d.Element("customername").Value
};
Anyone know what could be wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的印象是您在本地窗口中查看了错误的变量 - 您正在查看整个 XDocument,并且它的
.Value
属性确实类似于所有节点值的串联字符串......但是您确实应该查看
MyRows
变量 - 如果您确实正在执行您发布的代码,那么您应该看到这个:(完整图片)
这不是您正在寻找/期待的吗???
I have the impression you're looking at the wrong variable in your locals window - you're looking at the entire XDocument, and it's
.Value
property will indeed be something like a concatenated string of all node values.....But you really should look at the
MyRows
variable - and if you're really executing the code you posted, you should see this:(full picture here)
Isnt' that what you're looking for / expecting???
我刚刚运行了您的示例代码,并且修复了一些语法错误:
而且价值客户名称的逗号也是错误的,请尝试下面的我的版本,它应该可以工作。
I just run your sample code, and few syntax error were fixed:
and also a comma for the value customer name was wrong, try my version below, it should work.