如何使用 Linq 执行此 XPath 查询?

发布于 2024-09-05 15:13:24 字数 370 浏览 1 评论 0原文

在下面的代码中,我使用 XPath 来查找所有使用 XPath 匹配的节点,并将这些值附加到 StringBuilder。

StringBuilder sb = new StringBuilder();
foreach (XmlNode node in this.Data.SelectNodes("ID/item[@id=200]/DAT[1]/line[position()>1]/data[1]/text()"))
{
    sb.Append(node.Value);
}
return sb.ToString();

除了使用 Linq to XML 之外,我该如何做同样的事情?假设在新版本中,this.Data是一个XElement对象。

In the following code I am using XPath to find all of the matching nodes using XPath, and appending the values to a StringBuilder.

StringBuilder sb = new StringBuilder();
foreach (XmlNode node in this.Data.SelectNodes("ID/item[@id=200]/DAT[1]/line[position()>1]/data[1]/text()"))
{
    sb.Append(node.Value);
}
return sb.ToString();

How do I do the same thing, except using Linq to XML instead? Assume that in the new version, this.Data is an XElement object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海夜未眠 2024-09-12 15:13:24

您仍然可以将 XPath 与 XElement 结合使用。您必须包含 System.Xml.XPath 命名空间,因为它是扩展方法

var xml = XDocument.Parse("<xml></xml>");
var elements = xml.XPathSelectElements("ID/item[@id=200]/DAT[1]/line[position()>1]/data[1]/text()");

You can still use XPath with a XElement. You have to include the System.Xml.XPath namespace since its an extension method

var xml = XDocument.Parse("<xml></xml>");
var elements = xml.XPathSelectElements("ID/item[@id=200]/DAT[1]/line[position()>1]/data[1]/text()");
难以启齿的温柔 2024-09-12 15:13:24

查询语法看起来像这样。

var nodes = from item in Data.Elements("item")
            where item.Attribute("id").Value == "200"
            let dat = item.Element("DAT")
            from line in dat.Elements("line").Skip(1)
            let data = line.Element("data")
            select data;

var sb = new StringBuilder();
foreach (var node in nodes)
    sb.Append(node.Value);

...这将消除可能的 nullref 异常,并消除 foreach 循环。

var nodes = from item in Data.Elements("item")
            let id = item.Attribute("id")
            where id != null && id.Value == "200"
            let dat = item.Element("DAT")
            where dat != null
            from line in dat.Elements("line").Skip(1)
            let data = line.Element("data")
            where data != null
            select data.Value;
return nodes.Aggregate(new StringBuilder(), (sb, r) => sb.Append(r))
            .ToString();

Query syntax would look something like this.

var nodes = from item in Data.Elements("item")
            where item.Attribute("id").Value == "200"
            let dat = item.Element("DAT")
            from line in dat.Elements("line").Skip(1)
            let data = line.Element("data")
            select data;

var sb = new StringBuilder();
foreach (var node in nodes)
    sb.Append(node.Value);

... this would get rid of the possible nullref exceptions and would get rid of the foreach loop.

var nodes = from item in Data.Elements("item")
            let id = item.Attribute("id")
            where id != null && id.Value == "200"
            let dat = item.Element("DAT")
            where dat != null
            from line in dat.Elements("line").Skip(1)
            let data = line.Element("data")
            where data != null
            select data.Value;
return nodes.Aggregate(new StringBuilder(), (sb, r) => sb.Append(r))
            .ToString();
凉墨 2024-09-12 15:13:24

您可以尝试类似的方法:

var query = 
    from id in this.Data.Elements("ID")                      // ID
    from item in id.Elements("item")                         // /item
    where item.Attribute("id").Value == "200"                // [@id=200]
    let dat in item.Elements("DAT").ElementAtOrDefault(1)    // /DAT[1]
    where dat != null                                        // (ensure there is an element at index 1)
    from line in dat.Elements("line").Skip(1)                // /line[position()>1]
    let data in line.Elements("data").ElementAtOrDefault(1)  // /data[1]
    where data != null                                       // (ensure there is an element at index 1)
    select data                                              // /text()

StringBuilder sb = new StringBuilder();
foreach(XElement data in query)
{
    sb.Append(data.Value);
}
return sb.ToString();

如您所见,它并不比 XPath 更方便,因此您应该继续使用 XPath (如 Pierre-Alain 的回答中所述)

You can try something like that :

var query = 
    from id in this.Data.Elements("ID")                      // ID
    from item in id.Elements("item")                         // /item
    where item.Attribute("id").Value == "200"                // [@id=200]
    let dat in item.Elements("DAT").ElementAtOrDefault(1)    // /DAT[1]
    where dat != null                                        // (ensure there is an element at index 1)
    from line in dat.Elements("line").Skip(1)                // /line[position()>1]
    let data in line.Elements("data").ElementAtOrDefault(1)  // /data[1]
    where data != null                                       // (ensure there is an element at index 1)
    select data                                              // /text()

StringBuilder sb = new StringBuilder();
foreach(XElement data in query)
{
    sb.Append(data.Value);
}
return sb.ToString();

As you can see, it's not really more convenient than XPath, so you should probably keep using XPath (as explained in Pierre-Alain's answer)

帅的被狗咬 2024-09-12 15:13:24

我创建了一个开源帮助程序库,可让您使用 Linq-esq 表达式生成 xpath 表达式。不确定它对您的上下文是否有帮助,但将链接发布到 API

http://www.syntaxsuccess.com/viewarticle/how-to-create-xpath-using-linq

I have created an open source helper library that let's you generate xpath expressions using Linq-esq expressions. Not sure if it is helpful in your context, but posting the link to the API

http://www.syntaxsuccess.com/viewarticle/how-to-create-xpath-using-linq

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文