用LINQ提取 [英] Extract With LINQ
本文介绍了用LINQ提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有具有重复节点的xml:< STRUS name ="AB"
Have xml that has repeating node: <STRUS name="AB"
<STRUS name="AB" lineStart="3" lineEnd="33">
<SVALUs>
<SVALU name="TREE" line="33"><segment id="SE" xml:space="preserve"><element pos="1">34</element><element pos="2">100550212</element></segment></SVALU>
</SVALUs>
<STRUS name="1000A" lineStart="5" lineEnd="6">
<SVALUs>
<SVALU name="ZRT" line="5"><segment id="NM1" xml:space="preserve"><element pos="1">41</element>...<element pos="9">PCCARE</element></segment></SVALU>
<SVALU name="PER" line="6"><segment id="PER" xml:space="preserve"><element pos="1">IC</element>...<element pos="4">12345678</element></segment></SVALU>
</SVALUs>
</STRUS>
</STRUS>
<STRUS name="AB" lineStart="34" lineEnd="54">
<SVALUs>
<SVALU name="TREE" line="35"><segment id="SE" xml:space="preserve"><element pos="1">34</element><element pos="2">100550212</element></segment></SVALU>
</SVALUs>
<STRUS name="1000A" lineStart="35" lineEnd="36">
<SVALUs>
<SVALU name="ZRT" line="37"><segment id="NM1" xml:space="preserve"><element pos="1">41</element>...<element pos="9">TCOWS</element></segment></SVALU>
<SVALU name="PER" line="38"><segment id="PER" xml:space="preserve"><element pos="1">IC</element>...<element pos="4">12345678</element></segment></SVALU>
</SVALUs>
</STRUS>
</STRUS>
已经有使用xlinq遍历重复节点< STRUS name ="AB"
的代码
但无法从name ="ZRT"和pos ="9"中提取特定值来获取PCCARE和TCOWS:
您可以编辑现有代码吗?需要在以下重复循环代码中提取值:
Already have code that uses xlinq to loop through the repeating node <STRUS name="AB"
but unable to extract particular value from name="ZRT" and pos="9" to get PCCARE and TCOWS:
Can you edit existing code? Need to extract the values in repeating loop code below:
Dim xDoc As XDocument = XDocument.Load("E:\Some.xml")
Dim root As XElement = xDoc.Root
' Get the Node have STRU name="AB"
Dim STElems = From x In root.Descendants("STRUS") _
Where (x.Attribute("name").Value = "AB") _
Select x
' Iterate through each AB Node
For Each STElem As XElement In STElems
'How do you extract (PCCARE) for the node for line 18 where pos = 9
'Tried this but know it doesn't work:
Dim elements9 = From e In STElem.Descendants("SVALU") _
Where (e.Attribute("pos").Value = "9") And (e.Attribute("name").Value = "ZRT") _
Select e.Value
Dim ValueStr as STring = ?????
'First loop returns PCCARE and then second iteration returns TCOWS
Next
推荐答案
您的问题是属性为"pos"的节点是子节点SVALU节点的位置,但是您正在同一级别寻找它.
Your problem is the node with the attribute "pos" is a child of the SVALU node however you are looking for it at the same level
这篇关于用LINQ提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文