0

I am working on traversing an XML document. Here is a sample XML:

<DOCUMENT_ELEMENT FormName="Form Name">
  <Object type="Form name="form1">
    <Object type="BaseDesignPanel" name="Panel1">
      <Object type="FormsLabel" name="Label9">
        <Property name="BackColor">White</Property>
        <Property name="Name">Label9</Property>
        <Property name="Size">76, 16</Property>
        <Property name="Text">Page 1 of 3</Property>
        <Property name="Location">723, 985</Property>
      </Object>
      <Property name="AutoScrollMargin">800, 3000</Property>
      <Property name="IsWriteProtected">False</Property>
      <Property name="TabIndex">0</Property>
      <Property name="Size">800, 3000</Property>
      <Property name="Pages">3</Property>
      <Property name="BackColor">White</Property>
      <Property name="Tag">Please do not delete</Property>
      <Property name="Location">0, 0</Property>
      <Property name="AutoScrollMinSize">800, 3000</Property>
      <Property name="Name">Panel1</Property>
      <Property name="BackgroundImageLayout">None</Property>
    </Object>
    <Property name="MaximumSize">833, 1033</Property>
    <Property name="Name">form1</Property>
    <Property name="AutoScroll">True</Property>
    <Property name="ClientSize">817, 730</Property>
  </Object>
</DOCUMENT_ELEMENT>

I am using this piece of code to traverse recursively to each node.

Private Sub XmlDocumentWalker(XmlNode As XmlNode)
        If TypeOf XmlNode Is XmlComment Then
            Console.WriteLine("XML Comment:" + XmlNode.Value)
            If XmlNode.HasChildNodes Then XmlDocumentWalker(XmlNode.FirstChild)
            If Not IsNothing(XmlNode.NextSibling) Then XmlDocumentWalker(XmlNode.NextSibling)
        ElseIf TypeOf XmlNode Is XmlElement Then
            If XmlNode.HasChildNodes And (TypeOf XmlNode.FirstChild Is XmlText) Then
                Console.WriteLine("XML Element:" + XmlNode.Name & "=""" & XmlNode.FirstChild.Value & """")
            Else
                Console.WriteLine("XML Element: " + XmlNode.Name)
            End If
            If DirectCast(XmlNode, XmlElement).HasAttributes Then
                For Each XmlAttribute As XmlNode In DirectCast(XmlNode, XmlElement).Attributes
                    Console.WriteLine("XML Attribute: " + XmlAttribute.Name & "=""" & XmlAttribute.Value & """")
                Next
            End If

            If XmlNode.HasChildNodes And Not (TypeOf XmlNode.FirstChild Is XmlText) Then XmlDocumentWalker(XmlNode.FirstChild)
            If Not IsNothing(XmlNode.NextSibling) Then XmlDocumentWalker(XmlNode.NextSibling)
        End If
    End Sub

How can get the elements in a list of the corresponding element? If the node is on the Panel1 , I want to get the corresponding list of Elements.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Peter Sun
  • 1,675
  • 4
  • 27
  • 50

1 Answers1

0

I have only glanced at this but perhaps the following would help.

Dim nodeList As XmlNodeList = XMLDoc.SelectNodes("Case/ThirdParties/Details/Info")

            If nodeList.Count = 0 Then
                strReturn = "Fail: Please send at least one"
            Else
                Dim strBusinessName As String = String.Empty
                Dim strFirstName As String = String.Empty
                Dim strSurname As String = String.Empty

                For Each node As XmlNode In nodeList

                    NodeValue = node.SelectSingleNode("BusinessName")
                    If NodeValue IsNot Nothing Then
                        If node.SelectSingleNode("BusinessName").InnerXml <> "" Then
                            strBusinessName = Utilities.UCaseFirst(node.SelectSingleNode("BusinessName").InnerXml)
                        Else
                            strBusinessName = "-1"
                        End If
                    Else
                        strBusinessName = "-1"
                    End If