<Send> <Title>myTitle</Title> <User>username</User> <Transaction>106100</Transaction> <Items> <Item ID="12345" Name="myName" Birthday="myBirthday" Address="myAddress"/> </Items> </Send>
如果我有这种XML文档。如何在12345
代码中获取ID( <Item ... />
)的值?使用VB.Net?
答案 0 :(得分:0)
请尝试以下操作:
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:\temp\test2.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim items As List(Of Item) = doc.Descendants("Item").Select(Function(x) New Item() With { _
.id = x.Attribute("ID").Value, _
.name = x.Attribute("Name").Value, _
.birthday = x.Attribute("Birthday").Value, _
.address = x.Attribute("Address").Value _
}).ToList()
End Sub
End Module
Public Class Item
Public id As String
Public name As String
Public birthday As String
Public address As String
End Class
答案 1 :(得分:0)
这就是我想要的...
其中itemID
包含12345
_strMessage =
<Send>
<Title>myTitle</Title>
<User>username</User>
<Transaction>106100</Transaction>
<Items>
<Item ID="12345" Name="myName" Birthday="myBirthday" Address="myAddress"/>
</Items>
</Send>
Dim xMessage As XElement = XElement.Parse(_strMessage)
Dim items = xMessage.Descendants("Items").FirstOrDefault
Dim itemID As String = String.Empty
itemID = items.Descendants("Item").FirstOrDefault.Attribute("itemID").Value