XML 到 LINQ 元素选择
我有一个以下格式的 xml 文档。
<?xml version="1.0" encoding="UTF-8" ?>
<Rows>
<Row>
<Field Name='PhysicalLocation'>11;#West</Field>
<Field Name='ID'>3327</Field>
</Row>
</Rows>
我正在尝试用它进行 linq 选择。
我已经尝试过以下方法。
XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");
var query = from item in xmlDoc.Descendants("Rows").Elements()
select new { ID = item.Attribute("ID").Value, Value = item.Attribute("PhysicalLocation").Value };
而且
XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");
var query = from item in xmlDoc.Descendants("Rows").Elements()
select new { ID = item.Element("ID"), Value = item.Element("PhysicalLocation") };
在这两种情况下我似乎都达不到要求。它正在生成预期数量的行,但未填充值。
有人能指出我正确的方向吗?我缺少什么?
I have an xml document in the following format.
<?xml version="1.0" encoding="UTF-8" ?>
<Rows>
<Row>
<Field Name='PhysicalLocation'>11;#West</Field>
<Field Name='ID'>3327</Field>
</Row>
</Rows>
And am attempting to do a linq selection with it.
I have tried the following.
XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");
var query = from item in xmlDoc.Descendants("Rows").Elements()
select new { ID = item.Attribute("ID").Value, Value = item.Attribute("PhysicalLocation").Value };
And also
XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");
var query = from item in xmlDoc.Descendants("Rows").Elements()
select new { ID = item.Element("ID"), Value = item.Element("PhysicalLocation") };
And in both cases I seem to be coming up short. It is generating the expected amount of rows but the values are not being filled.
Could anyone point me in the right direction? What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有名为“PhysicalLocation”或“ID”的属性。您只有名为“名称”的属性。
您需要在“name”属性的值上添加一个 where 子句来查找您的 ID 和 PhysicalLocation
you don't have an attribute called 'PhysicalLocation' or 'ID'. you only have attributes called 'Name'.
You need to add a where clause on the value of the 'name' attribute to find your ID and PhysicalLocation
我认为这应该可以解决问题:
这是循环打印的结果:
I think this should do the trick:
Here is the result printed by the loop:
试试这个:
上面的代码循环遍历每个“Row”元素,然后读取“Field”元素数据。它将返回以下匿名列表:
要循环查询,可以使用以下代码:
最后,要通过
Name
查找Field 元素值
,可以使用以下查询:Try this:
The above code, loops through each "Row" element, then reads the "Field" element data. It will return the following anonymous list:
To loop through the query, you can use the following code:
Finally, to find the
Field element value
byName
, you can use the following query:尝试这样的查询怎么样:
How about trying a query like this: