LINQ to XML 相当于 XPath
我有解析 XML 的代码,如下所示:
<custom_fields>
<custom_field>
<column_name>foo</column_name>
<column_value>0</column_value>
<description>Submitted</description>
<data_type>BOOLEAN</data_type>
<length>0</length>
<decimal>0</decimal>
</custom_field>
<custom_field>
<column_name>bar</column_name>
<column_value>0</column_value>
<description>Validated</description>
<data_type>BOOLEAN</data_type>
<length>0</length>
<decimal>0</decimal>
</custom_field>
</custom_fields>
... more <custom_field> elements...
我想找到名为 custom_field
的元素,它有一个名为 column_name
的子元素,具有特定值(例如 bar
),然后找到名为 column_value
的子级同级并获取其值。现在,我在 XMlDocument
上使用 XPath 来执行此操作:
string path = "//custom_fields/custom_field[column_name='" + key + "']";
XmlNode xNode = doc.SelectSingleNode(path);
if (xNode != null)
{
XmlNode v = xNode.SelectSingleNode("column_value");
val.SetValue(v.InnerText);
}
其中 key
是我要查找的字段的名称。
但我想在 XDocument
上使用新的 LINQ to XML 语法来执行此操作。我的想法是,我将把大部分旧式 XPath 解析转移到 LINQ 方法。也许这不是一个好主意,但在这种情况下,如果我能让它工作,那么我相信我会对 LINQ 总体有更好的理解,并且能够清理很多复杂的代码。
I have code which parses XML that looks like this:
<custom_fields>
<custom_field>
<column_name>foo</column_name>
<column_value>0</column_value>
<description>Submitted</description>
<data_type>BOOLEAN</data_type>
<length>0</length>
<decimal>0</decimal>
</custom_field>
<custom_field>
<column_name>bar</column_name>
<column_value>0</column_value>
<description>Validated</description>
<data_type>BOOLEAN</data_type>
<length>0</length>
<decimal>0</decimal>
</custom_field>
</custom_fields>
... more <custom_field> elements...
I want to find the element called custom_field
which has a child element called column_name
with a certain value (for example bar
), and then find that child's sibling called column_value
and get its value. Right now I use XPath on an XMlDocument
to do this:
string path = "//custom_fields/custom_field[column_name='" + key + "']";
XmlNode xNode = doc.SelectSingleNode(path);
if (xNode != null)
{
XmlNode v = xNode.SelectSingleNode("column_value");
val.SetValue(v.InnerText);
}
Where key
is the name of the field I am looking for.
But I want to do this using the new LINQ to XML syntax on an XDocument
. My thinking is that I will move much of my old-style XPath parsing to the LINQ methods. Maybe it's not a good idea, but this is a case where if I can get it to work, then I believe I will have a much better understanding of LINQ in general, and will be able to clean up a lot of complex code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您始终可以在 LINQ to XML 中使用 XPath。只需包含 System.Xml.XPath 命名空间即可。
否则,对于等效的 LINQ to XML 查询:
You can always use XPath within LINQ to XML. Just include the
System.Xml.XPath
namespace.Otherwise for the equivalent LINQ to XML query:
您的 XQuery 表达式
选择
custom_fields
元素中的所有custom_field
元素,其中column_key
子元素的值等于"key"
。您希望返回单个元素并选择column_value
子元素的值。您可以使用 LINQ to XML 来表达这一点,如下所示:
Your XQuery expression
selects all
custom_field
elements incustom_fields
elements where the value of thecolumn_key
child element equals"key"
. You expect a single element to be returned and select the value of thecolumn_value
child element.You can express this using LINQ to XML as follows:
使用:
Use: