XML文件的格式
<Main> <Item> <ProductDescription ID="123" LongDescription="Text" Langid="1"> </Item> </Main>我试图从XML文件中获取LongDescription值(文本)
我使用的代码
Dim doc As New XmlDocument() doc.Load(path) Dim xNList As XmlNodeList = doc.SelectNodes("//Main/Item/ProductDescription") For Each xNode As XmlNode In xNList MsgBox(xNode.OuterXml) Next我得到的结果是
<ProductDescription ID="123" LongDescription="Text" Langid="1">但我只是想提前感谢值Text
对不起打字错误
Format of XML File
<Main> <Item> <ProductDescription ID="123" LongDescription="Text" Langid="1"> </Item> </Main>And i am trying to get LongDescription value (Text) from the XML file
Code i use
Dim doc As New XmlDocument() doc.Load(path) Dim xNList As XmlNodeList = doc.SelectNodes("//Main/Item/ProductDescription") For Each xNode As XmlNode In xNList MsgBox(xNode.OuterXml) NextResult i get is
<ProductDescription ID="123" LongDescription="Text" Langid="1">But i just want the value Text thanks in advance
Sorry for typos
最满意答案
您已经拥有xNode的元素。 您所要做的就是访问LongDescription属性。 最简单的方法是将xNode为XmlElement并使用GetAttribute -method:
Dim doc As New XmlDocument() doc.Load(path) Dim xNList As XmlNodeList = doc.SelectNodes("//Main/Item/ProductDescription") For Each xNode As XmlNode In xNList Dim longDesc = DirectCast(xNode, XmlElement).GetAttribute("LongDescription") MsgBox(longDesc) NextYou already have the element in xNode. All you have to do is to access the attribute LongDescription. The easiest way is to cast xNode to XmlElement and use the GetAttribute-method:
Dim doc As New XmlDocument() doc.Load(path) Dim xNList As XmlNodeList = doc.SelectNodes("//Main/Item/ProductDescription") For Each xNode As XmlNode In xNList Dim longDesc = DirectCast(xNode, XmlElement).GetAttribute("LongDescription") MsgBox(longDesc) Next更多推荐
Text,LongDescription,XML,As,doc,电脑培训,计算机培训,IT培训"/> <meta name=&qu
发布评论