【问题标题】:Search certain value in descendants with LINQ to XML with VB.Net使用 VB.Net 使用 LINQ to XML 在后代中搜索特定值
【发布时间】:2018-04-20 09:12:55
【问题描述】:

这是我正在处理的 xml 文件的摘要

<?xml version="1.0" encoding="utf-8"?>
<Devoluciones>
  <Remesa>
    <Archivo>PRE20180403140755376539AD95PSPTJ7E6</Archivo>
    <TxInfAndSts>
      <OrgnlInstrId>PRE20180403140755376539AD95PSPTJ7E6</OrgnlInstrId>
      <OrgnlEndToEndId>5099</OrgnlEndToEndId>
      <StsRsnInf>
        <Rsn>
         <Cd>MD01</Cd>
        </Rsn>
      </StsRsnInf>
    </TxInfAndSts>
  </Remesa>
  <Remesa>
    <Archivo>PRE201804031123897319287123098AC78C</Archivo>
    <TxInfAndSts>
      <OrgnlInstrId>PRE201804031123897319287123098AC78C</OrgnlInstrId>
      <OrgnlEndToEndId>2141</OrgnlEndToEndId>
      <StsRsnInf>
        <Rsn>
          <Cd>MD01</Cd>
        </Rsn>
      </StsRsnInf>
    </TxInfAndSts>
    <TxInfAndSts>
      <OrgnlInstrId>PRE201804031123897319287123098AC78C</OrgnlInstrId>
      <OrgnlEndToEndId>2313</OrgnlEndToEndId>
      <StsRsnInf>
        <Rsn>
          <Cd>AC04</Cd>
        </Rsn>
      </StsRsnInf>
    </TxInfAndSts>
  </Remesa>
</Devoluciones>

我正在尝试使用下面的代码在具有特定值的父母中找到标签 Cd 的值。它有效,但我认为它可以简化。

    Dim archivoSEPA As String = "PRE201804031123897319287123098AC78C"
    Dim RefAdeudo As Integer = "2313"
    Dim xml As XElement = XElement.Load(path)
    Dim OrgnlInstrId, OrgnlEndToEndId, TxInfAndSts As XElement
    Dim dev As String = String.Empty

    If xml.Descendants.Where(Function(el) el.Name.LocalName = "OrgnlInstrId" And el.Value = archivoSEPA).Count <> 0 Then
        OrgnlInstrId = xml.Descendants.Where(Function(el) el.Name.LocalName = "OrgnlInstrId" And CStr(el.Value) = archivoSEPA).First
        If OrgnlInstrId.Parent.Descendants.Where(Function(el) el.Name.LocalName = "OrgnlEndToEndId" And el.Value = RefAdeudo.ToString).Count <> 0 Then
            OrgnlEndToEndId = OrgnlInstrId.Parent.Descendants.Where(Function(el) el.Name.LocalName = "OrgnlEndToEndId" And el.Value = RefAdeudo.ToString).First
            TxInfAndSts = OrgnlEndToEndId.Parent
            dev = TxInfAndSts.Descendants.Where(Function(el) el.Name.LocalName = "StsRsnInf").First _
                .Descendants.Where(Function(el) el.Name.LocalName = "Rsn").First _
                .Descendants.Where(Function(el) el.Name.LocalName = "Cd").Value
        End If
    End If

非常感谢(对不起我的英语!)

【问题讨论】:

    标签: vb.net linq-to-xml


    【解决方案1】:

    是的,您的查询可以简化...这里有一个单行:

    Dim q = xdoc...<TxInfAndSts>.Where(Function(c) c.<OrgnlInstrId>.Count > 0 And c.<OrgnlInstrId>.First.Value = archivoSEPA).Where(Function(c) c.<OrgnlEndToEndId>.Count > 0 And c.<OrgnlEndToEndId>.First.Value = RefAdeudo)
    

    以下是供审查的样本存根:

    Dim xdoc As XDocument = <?xml version="1.0" encoding="utf-8"?>
                                        <Devoluciones>
                                            <Remesa>
                                                <Archivo>PRE20180403140755376539AD95PSPTJ7E6</Archivo>
                                                <TxInfAndSts>
                                                    <OrgnlInstrId>PRE20180403140755376539AD95PSPTJ7E6</OrgnlInstrId>
                                                    <OrgnlEndToEndId>5099</OrgnlEndToEndId>
                                                    <StsRsnInf>
                                                        <Rsn>
                                                            <Cd>MD01</Cd>
                                                        </Rsn>
                                                    </StsRsnInf>
                                                </TxInfAndSts>
                                            </Remesa>
                                            <Remesa>
                                                <Archivo>PRE201804031123897319287123098AC78C</Archivo>
                                                <TxInfAndSts>
                                                    <OrgnlInstrId>PRE201804031123897319287123098AC78C</OrgnlInstrId>
                                                    <OrgnlEndToEndId>2141</OrgnlEndToEndId>
                                                    <StsRsnInf>
                                                        <Rsn>
                                                            <Cd>MD01</Cd>
                                                        </Rsn>
                                                    </StsRsnInf>
                                                </TxInfAndSts>
                                                <TxInfAndSts>
                                                    <OrgnlInstrId>PRE201804031123897319287123098AC78C</OrgnlInstrId>
                                                    <OrgnlEndToEndId>2313</OrgnlEndToEndId>
                                                    <StsRsnInf>
                                                        <Rsn>
                                                            <Cd>AC04</Cd>
                                                        </Rsn>
                                                    </StsRsnInf>
                                                </TxInfAndSts>
                                            </Remesa>
                                        </Devoluciones>
                Dim archivoSEPA$ = "PRE201804031123897319287123098AC78C", RefAdeudo% = 2313
                Dim q = xdoc...<TxInfAndSts>.Where(Function(c) c.<OrgnlInstrId>.Count > 0 And c.<OrgnlInstrId>.First.Value = archivoSEPA).Where(Function(c) c.<OrgnlEndToEndId>.Count > 0 And c.<OrgnlEndToEndId>.First.Value = RefAdeudo)
                If q.Count > 0 Then
                    MsgBox(IIf(q.First...<Cd>.Count > 0, q.First...<Cd>.First.Value, "No such element found!"))
                End If
    

    希望这会有所帮助...

    【讨论】:

    • 谢谢,它有效!但是我忘记在示例中添加一个属性。标签 TxInfAndSts 始终具有此属性: 那么它不会使用该代码返回结果
    • 在阅读之前,我已经删除了 XML 中的所有属性(我不需要它们),所以它知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多