【发布时间】: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