【问题标题】:I need to extract data from an XML document我需要从 XML 文档中提取数据
【发布时间】:2012-07-13 09:56:24
【问题描述】:

您好,我无法从此 XML 文档中提取数据。

  <messages messageCount="6">
  <message messageID="9999" orderNumber="1234" model="TESTMODEL" val="490" status="8" timestamp="2012-07-12 13:12:50Z">
  <attributes>
  <attribute name="ATT1" value="1234" /> 
  <attribute name="ATT2" value="5678" /> 
  </attributes>
  </message>
  </messages>

我需要递归循环每条消息并获取消息节点的值。然后,如果状态为某个值,我需要遍历属性并获取属性节点的值。我有点麻烦。到目前为止我有这个

        Dim strStream As New MemoryStream(System.Text.Encoding.UTF8.GetBytes(strMessage))

        Dim XmlDoc As XmlDocument = New XmlDocument

        XmlDoc.Load(strStream)


        Dim nlNodeList As XmlNodeList = XmlDoc.SelectNodes("//messages/message")
        Dim a As XmlAttribute

        For Each nNode As XmlNode In nlNodeList
            Dim strmessageID As String = String.Empty
            For Each a In nNode.Attributes
                If a.Name = "messageID" Then
                    strmessageID = a.Value
                End If
            Next
            For Each nChildNode As XmlNode In nNode.ChildNodes
                For Each b In nChildNode.ChildNodes
                    For Each a In b.Attributes
                        If a.Name = "ATT1" Then

                        End If
                    Next
                Next
            Next
        Next

但我无法获取属性值。我敢肯定也必须有一种更清洁的方法。我以前使用过数据集,在我尝试获取属性值之前一直很好

    For Each dr As DataRow In dsmyDS.Tables("message").Rows
        Dim strMessageID As String = dr.Item("messageid").ToString
        Select Case CStr(dr.Item("model").ToString)
            Case "TESTMODEL"
                Select Case dr.Item("status").ToString
                    Case "8"
                        Dim strval As String = dr.Item("val").ToString
                        'Don't know how to get at the attributes node list once I'm here
                    Case Else

                End Select
            Case Else

        End Select
    Next

如果有人能告诉我我做错了什么,那就太好了。哪个是最好的使用方法? XMLDocument 还是数据集?有没有比我冗长的方法更简单的方法?

任何帮助将非常感谢!

【问题讨论】:

  • 我认为您对属性和名称为“属性”的元素感到困惑;而且您还会对元素的名称和名称为“Name”的元素的属性值感到困惑。

标签: xml vb.net xml-parsing


【解决方案1】:

您应该尝试LINQ-XML 导入 System.Data.Xml。

 Dim doc As XDocument = XDocument.Load(file)
'Or
'Dim doc As XDocument = XDocument.Parse(strMessage)
 Dim Result = doc.Root.Descendants("message")
                      .Where(Function(p)
                                Return p.Attribute("status").Value = "8"
                             End Function)
 For Each ele In Result
     MsgBox(ele.Attribute("messageID").Value)
     MsgBox(ele.Element("attributes").Element("attribute").Attribute("name").Value)

     'List children of attributes tag
     For Each v In ele.Element("attributes").Elements()
          MsgBox(v.Attribute("name").Value & " " & v.Attribute("value").Value)
     Next
 Next

【讨论】:

  • 嗨,AVD。我传入的是流而不是文件,并收到错误“缺少根元素”。当我到达 XDocument.Load。
  • 你必须导入System.Xml.Linq并使用XDocument.Parse(stringXml)来加载xmlstring。
  • 您,好心的先生,是个传奇。非常感谢!我真的需要复习一下我的 XML 技能!
  • 最后一个问题。您给出的示例只会给出 top 属性。在我的示例“ATT1”中。如何循环遍历每个“ele”的属性节点列表。或者我如何只选择名称与某个字符串匹配的属性,并返回该属性的值?
  • 我将您的代码更改为仅使用您的两个示例的组合搜索我正在寻找的子节点。不知道如何在 cmets 中发布代码,但我使用了
    For Each v In ele.Element("attributes").Elements().Where(Function(s)
    Return s.Attribute("name").Value = "OTDAT"
    End Function)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
相关资源
最近更新 更多