在节点中查找XML节点? [英] Find XML nodes within nodes?

查看:68
本文介绍了在节点中查找XML节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在努力解决如何提取xml节点的内部文本值,其中父节点并不总是包含相同的子节点。我的(修剪版)xml文件如下。我正在尝试从存在它们的每个PointRecord节点中提取属性值。属性可能存在也可能不存在。



我只想在属性存在时提取PointRecord'Name'值和所有Attribute值。 :(任何想法?请原谅我,因为我有点像菜鸟..



 <   PointRecord     ID   =  00000059   < span class =code-attribute> TimeStamp   =  2019-03-16T11:53:43  >  
< 姓名 > 1A < /姓名 >
< 方法 > DirectReading < ; / Method >
< 功能 >
< 功能 名称 = elCABL >
< 属性 >
< 名称 > DSS代码< /姓名 >
< > 21 < / Value >
< / Attribute >
< 属性 >
< 名称 > 深度( m)< / Name >
< 价值 > 0.8 < / Value >
< / Attribute >
< 属性 >
< 名称 > < /姓名 >
< > B < / Value >
< / Attribute >
< / Feature >
< /功能 >
< span class =code-keyword>< / PointRecord >

< PointRecord ID = 00000060 TimeStamp = 2019-03-16T11:54:33 >
< 名称 > 1B < /姓名 >
< 方法 > DirectReading < / Method >
< / PointRecord >

< PointRecord ID = 00000061 TimeStamp < span class =code-keyword> = 2019-03-16T11:55:33 > ;
< 名称 > 1C < / Name > ;
< 方法 > DirectReading < / Method > ;
< 功能 >
< 功能 名称 = elPole >
< 属性 >
< 名称 > DSS代码< /姓名 >
< > 23 < / Value >
< / Attribute >
< /功能 >
< /功能 >
< span class =code-keyword>< / PointRecord >





我尝试过:



 Dim aux As New XmlDocument()
Dim PointLst As XmlNodeList
Dim Name as String = String.Empty
aux.Load(test.xml)

PointLst = aux.GetElementsByTagName(PointRecord)

每个Att作为PointLst中的XmlElement

如果Att.GetElementsByTagName(Feature)IsNot Nothing Then

Name = Att.FirstCild.InnerText
//在这里丢失,不确定如何处理?
结束如果
下一步

 

解决方案

< blockquote>我更喜欢使用 XDocument类 [ ^ ]。





  Dim  result = xdoc.Root.Descendants(  Feature) _ 
.SelectMany( Function (x)x.Descendants( 属性)_
选择功能( y) 使用 _
{_
.Future = x.Attribute( 名称)。值,_
.Name = y.Element( 名称)。值,_
.Value = y.Element( < span class =code-string> Value)。Value _
})_
)_
.ToList()





结果:

未来名称价值
elCABL DSS代码21
elCABL深度(m )0.8
elCABL B类
elPole DSS代码23


Hi,

I am struggling to get my head around how to extract inner text values of xml nodes where the parent node does not always contain the same child node. My (trimmed down version) xml file is below. I'm trying to extract the Attribute values from each PointRecord node where they exist. The Attribute may or may not exist.

I only want to extract the PointRecord 'Name' value and all the Attribute values only if the Attributes exist. :( Any ideas? Forgive me as as I'm a bit of a noob..

<PointRecord ID="00000059" TimeStamp="2019-03-16T11:53:43">
            <Name>1A</Name>
            <Method>DirectReading</Method>
            <Features>
                <Feature Name="elCABL">
                    <Attribute>
                        <Name>DSS Code</Name>
                        <Value>21</Value>
                    </Attribute>
                    <Attribute>
                        <Name>Depth (m)</Name>
                        <Value>0.8</Value>
                    </Attribute>
                    <Attribute>
                        <Name>Class</Name>
                        <Value>B</Value>
                    </Attribute>
                </Feature>
            </Features>
</PointRecord>

<PointRecord ID="00000060" TimeStamp="2019-03-16T11:54:33">
            <Name>1B</Name>
            <Method>DirectReading</Method>
</PointRecord>

<PointRecord ID="00000061" TimeStamp="2019-03-16T11:55:33">
            <Name>1C</Name>
            <Method>DirectReading</Method>
            <Features>
                <Feature Name="elPole">
                    <Attribute>
                        <Name>DSS Code</Name>
                        <Value>23</Value>
                    </Attribute>
                </Feature>
            </Features>
</PointRecord>



What I have tried:

Dim aux As New XmlDocument()
Dim PointLst As XmlNodeList        
Dim Name as String = String.Empty
aux.Load("test.xml")

PointLst = aux.GetElementsByTagName("PointRecord")

    For Each Att as XmlElement in PointLst

        If Att.GetElementsByTagName("Feature") IsNot Nothing Then

            Name = Att.FirstCild.InnerText
            //lost here, not sure how to procees??
        End If
    Next

解决方案

I prefer to use XDocument class[^].


Dim result = xdoc.Root.Descendants("Feature") _
	.SelectMany(Function(x) x.Descendants("Attribute") _
		.Select(Function(y) New With _
		{ _
			.Future = x.Attribute("Name").Value, _
			.Name = y.Element("Name").Value, _
			.Value = y.Element("Value").Value _
		}) _
	) _
	.ToList()



Result:

Future Name      Value
elCABL DSS Code  21 
elCABL Depth (m) 0.8 
elCABL Class     B 
elPole DSS Code  23 


这篇关于在节点中查找XML节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆