I have the following xml:
我有以下xml:
<root ...>
<Tables>
<Table content="..">
</Table>
<Table content="interesting">
<Item ...></Item>
<Item ...></Item>
<Item ...></Item>
</Table>
...etc...
</Tables>
</root>
I'm using the following code to get the items from the 'interesting' node:
我使用以下代码从“有趣”节点获取项目:
XElement xel = XElement.Parse(resp);
var nodes = from n in xel.Elements("Tables").Elements("Table")
where n.Attribute("content").Value == "interesting"
select n;
var items = from i in nodes.Elements()
select i;
Is there a simpler, cleaner way to achieve this?
有没有更简单、更干净的方法来实现这个目标?
4 个解决方案
#1
5
Well there's no point in using a query expression for items
, and you can wrap the whole thing up very easily in a single statement. I wouldn't even bother with a query expression for that:
对项使用查询表达式是没有意义的,您可以很容易地将整个内容封装在一个语句中。我甚至不需要查询表达式:
var items = XElement.Parse(resp)
.Elements("Tables")
.Elements("Table")
.Where(n => n.Attribute("content").Value == "interesting")
.Elements();
Note that this (and your current query) will throw an exception for any Table
element without a content
attribute. If you'd rather just skip it, you can use:
注意,这(以及您当前的查询)将为任何没有content属性的表元素抛出异常。如果你宁愿跳过它,你可以使用:
.Where(n => (string) n.Attribute("content") == "interesting")
instead.
代替。
#2
2
You can use XPath (extension is in System.Xml.XPath namespace) to select all items in one line:
可以使用XPath(扩展名在System.Xml中。选择一行中的所有项:
var items = xel.XPathSelectElements("//Table[@content='interesting']/Item");
#3
1
If you don't need nodes
outside of your query for items
, you can just do this:
如果在查询项之外不需要节点,可以这样做:
var items = from n in xel.Elements("Tables").Elements("Table")
where n.Attribute("content").Value == "interesting"
from i in n.Elements()
select i;
#4
1
using xml document
XmlDocument xdoc = new XmlDocument();
使用xml文档XmlDocument xdoc = new XmlDocument();
var item= xdoc.GetElementsByTagName("Table[@content='interesting']/Item");
var = xdoc.GetElementsByTagName项(“表[@content = '有趣']/项目”);
#1
5
Well there's no point in using a query expression for items
, and you can wrap the whole thing up very easily in a single statement. I wouldn't even bother with a query expression for that:
对项使用查询表达式是没有意义的,您可以很容易地将整个内容封装在一个语句中。我甚至不需要查询表达式:
var items = XElement.Parse(resp)
.Elements("Tables")
.Elements("Table")
.Where(n => n.Attribute("content").Value == "interesting")
.Elements();
Note that this (and your current query) will throw an exception for any Table
element without a content
attribute. If you'd rather just skip it, you can use:
注意,这(以及您当前的查询)将为任何没有content属性的表元素抛出异常。如果你宁愿跳过它,你可以使用:
.Where(n => (string) n.Attribute("content") == "interesting")
instead.
代替。
#2
2
You can use XPath (extension is in System.Xml.XPath namespace) to select all items in one line:
可以使用XPath(扩展名在System.Xml中。选择一行中的所有项:
var items = xel.XPathSelectElements("//Table[@content='interesting']/Item");
#3
1
If you don't need nodes
outside of your query for items
, you can just do this:
如果在查询项之外不需要节点,可以这样做:
var items = from n in xel.Elements("Tables").Elements("Table")
where n.Attribute("content").Value == "interesting"
from i in n.Elements()
select i;
#4
1
using xml document
XmlDocument xdoc = new XmlDocument();
使用xml文档XmlDocument xdoc = new XmlDocument();
var item= xdoc.GetElementsByTagName("Table[@content='interesting']/Item");
var = xdoc.GetElementsByTagName项(“表[@content = '有趣']/项目”);