【问题标题】:Linq to xml, when child nodes meet a certain conditionlinq to xml,当子节点满足一定条件时
【发布时间】:2014-06-30 02:41:23
【问题描述】:

假设我有下一个xml:

<Report>
<File id="1">
    <Variables>
            <Variable id="1" name="integer"> 1 </Variable>
            <Variable id="1" name="string"> x </Variable>
    </Variables>
</File>
<File id="2">
    <Variables>
            <Variable id="2" name="integer"> 1 </Variable>
            <Variable id="2" name="string"> x </Variable>
    </Variables>
</File>
<File id="3">
    <Variables>
            <Variable id="3" name="integer"> 1 </Variable>
            <Variable id="3" name="string"> y </Variable>
    </Variables>
</File>

如何获取变量字符串为“x”的所有文件?

注意 linq to xml vb 或 c# 但首选 vb

【问题讨论】:

  • 请在提问时展示您尝试过/没有奏效的内容,而不是在帖子中填写谢谢/搜索了很多不相关的文字。

标签: c# vb.net linq linq-to-xml


【解决方案1】:

假设您已经将 XML 加载到 XDocument 实例,您可以执行以下操作:

var files = from f in xDoc.Root.Elements("File")
            where f.Element("Variables")
                   .Elements("Variable")
                   .Any(v => (string)v.Attribute == "string" &&
                             (string)v == "x")
            select f;

或使用等效的基于方法的查询:

var files = xDoc.Root.Elements("File")
                     .Where(f => f.Element("Variables")
                                  .Elements("Variable")
                                  .Any(v => (string)v.Attribute == "string" &&
                                            (string)v == "x"))

【讨论】:

    【解决方案2】:

    给定以下 xml:

    Dim xml = <Report>
                    <File id="1">
                        <Variables>
                                <Variable id="1" name="integer">1</Variable>
                                <Variable id="1" name="string">x</Variable>
                        </Variables>
                    </File>
                    <File id="2">
                        <Variables>
                                <Variable id="2" name="integer">1</Variable>
                                <Variable id="2" name="string">x</Variable>
                        </Variables>
                    </File>
                    <File id="3">
                        <Variables>
                                <Variable id="3" name="integer">1</Variable>
                                <Variable id="3" name="string">y</Variable>
                        </Variables>
                    </File>
                    </Report>
    

    您可以使用 Linq:

    Dim result = xml.<File> _
                    .Where(Function(file) file.<Variables>.<Variable> _
                      .Any(Function(variable) variable.@name = "string" AndAlso 
                                              variable.Value = "x"))
    

    或者,简而言之,使用 XPath-Expression 查询节点:

    Dim doc = New XmlDocument() ' Use XmlDocument instead of XElement '
    doc.LoadXml(xml.ToString())
    Dim result = doc.SelectNodes("/Report/File[Variables/Variable[@name='string' and text()='x']]")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多