我的xml如下:
<Team>
<Member Name="Alex" Info="<Details Weight="80" Category="Fighter" LastFight="2014-03-01" />"/>
</Team>
我想找到一种方法来直接加载Attribute LastFight而不会创建大量的xmldocuments。这是我使用的代码:
Dim storagexml As New XmlDocument
storagexml.LoadXml(<Team><Member Name="Alex" Info="<Details Weight="80" Category="Fighter" LastFight="2014-03-01" />"/></Team>)
Dim tempnodelist As XmlNodeList = storagexml.SelectNodes("Team/Member")
For Each tempnode As XmlNode In tempnodelist
If tempnode.Attributes("Name") IsNot Nothing Then
Dim tempdoc As New XmlDocument
tempdoc.LoadXml(tempnode.Attributes("Info").Value)
Dim tempsel As XmlNode = tempdoc.SelectSingleNode("Info")
If Not tempsel.Attributes("LastFight").Value.Trim() = "" Then
RichTextBox1.AppendText(tempnode.Attributes("Name").Value & " " & tempsel.Attributes("LastFight").Value & vbCrLf)
End If
答案 0 :(得分:0)
Info
属性的值是另一个XML字符串,因此,据我所知,您无法将其加载到另一个XML抽象对象(f.e XmlDocument
)。
无论如何,您可以使用LINQ-to-XML简化代码。例如,使用XElement
和VB.NET特定功能来访问XML内联部分:
Dim xml = <Team>
<Member Name="Alex" Info="<Details Weight="80" Category="Fighter" LastFight="2014-03-01" />"/>
</Team>
'notice how to access child element and attribute inline :
'
Dim info = XElement.Parse(xml.<Member>.@Info)
Dim lastFight As String = info.@LastFight
Console.WriteLine(lastFight)