locked
how to read name of child nodes RRS feed

  • Question

  • <ClaimData>
      <FNOL>
        <PolicyDetails>
          <PolicyNumber>123456</PolicyNumber>
          <InsuredName>LIFE INS</InsuredName>
          <EffectiveDate>?</EffectiveDate>
          <ExpirationDate>?</ExpirationDate>
        </PolicyDetails>
        <Newclaim>
          <LossDate>04/04/2018</LossDate>
          <typeofclaim>Liability</typeofclaim>
        </Newclaim>

    <FNOL>

    <ClaimData>

    Tuesday, May 1, 2018 4:43 AM

All replies

  •         Dim XMLElements As XmlNodeList = doc.GetElementsByTagName("PolicyDetails")
    
            For Each item As XmlNode In XMLElements
                If item.HasChildNodes Then
                    Dim i As Integer
                    For i = 0 To item.ChildNodes.Count - 1
                        Console.WriteLine("Node Name        :" + item.ChildNodes(i).Name)
                        Console.WriteLine("Node InnerText   :" + item.ChildNodes(i).InnerText)
                    Next i
                End If
            Next
    https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes%28v=vs.110%29.aspx
    Tuesday, May 1, 2018 6:09 AM
  • Maybe this if reading from a file or string.

    Also see XmlReader Class

    Option Strict On
    
    Imports System.Xml
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim xml As New Xml.XmlTextReader("C:\Users\John\Desktop\XML Test.Xml")
            Dim s As String = ""
            Dim t As String = ""
            While xml.Read
                If xml.NodeType = System.Xml.XmlNodeType.Element Then
                    t = xml.Name
                ElseIf xml.NodeType = System.Xml.XmlNodeType.Text Then
                    s &= "Node name = " & t & "| Node value = " & xml.Value & vbCrLf
                End If
            End While
            TextBox1.Text = s
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim r As String = "<ClaimData><FNOL><PolicyDetails><PolicyNumber>123456</PolicyNumber><InsuredName>LIFE INS</InsuredName>" & _
                "<EffectiveDate>?</EffectiveDate><ExpirationDate>?</ExpirationDate></PolicyDetails><Newclaim><LossDate>04/04/2018</LossDate>" & _
                "<typeofclaim>Liability</typeofclaim></Newclaim></FNOL></ClaimData>"
            Dim s As String = ""
            Dim t As String = ""
            Using ms As New IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(r))
                Dim xml As Xml.XmlReader = XmlReader.Create(ms)
                While xml.Read
                    If xml.NodeType = System.Xml.XmlNodeType.Element Then
                        t = xml.Name
                    ElseIf xml.NodeType = System.Xml.XmlNodeType.Text Then
                        s &= "Node name = " & t & "| Node value = " & xml.Value & vbCrLf
                    End If
                End While
                TextBox1.Text = s
            End Using
        End Sub
    
    End Class

    <ClaimData>
       <FNOL>
         <PolicyDetails>
           <PolicyNumber>123456</PolicyNumber>
           <InsuredName>LIFE INS</InsuredName>
           <EffectiveDate>?</EffectiveDate>
           <ExpirationDate>?</ExpirationDate>
         </PolicyDetails>
         <Newclaim>
           <LossDate>04/04/2018</LossDate>
           <typeofclaim>Liability</typeofclaim>
         </Newclaim>
      </FNOL>
    </ClaimData>


    La vida loca

    Tuesday, May 1, 2018 11:34 AM
  • This show how to read each name, but I wonder if your question isn't more.

            Dim xe As XElement
            'xe = XElement.Load("path / URI here") 'to load
            'for testing you can do this
            xe = <ClaimData>
                     <FNOL>
                         <PolicyDetails>
                             <PolicyNumber>123456</PolicyNumber>
                             <InsuredName>LIFE INS</InsuredName>
                             <EffectiveDate>?</EffectiveDate>
                             <ExpirationDate>?</ExpirationDate>
                         </PolicyDetails>
                         <Newclaim>
                             <LossDate>04/04/2018</LossDate>
                             <typeofclaim>Liability</typeofclaim>
                         </Newclaim>
                     </FNOL>
                 </ClaimData>
    
            For Each el As XElement In xe.DescendantsAndSelf
                Debug.WriteLine("Name: {0}", el.Name)
            Next
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Tuesday, May 1, 2018 1:04 PM
  • Hi Nikhil4991,

    Try this:

      Dim xml As String = "<ClaimData>
       <FNOL>
         <PolicyDetails>
           <PolicyNumber>123456</PolicyNumber>
           <InsuredName>LIFE INS</InsuredName>
           <EffectiveDate>?</EffectiveDate>
           <ExpirationDate>?</ExpirationDate>
         </PolicyDetails>
         <Newclaim>
           <LossDate>04/04/2018</LossDate>
           <typeofclaim>Liability</typeofclaim>
         </Newclaim>
    </FNOL>
    </ClaimData>"
            Dim xmldoc As New XmlDocument
            xmldoc.LoadXml(xml)
            Dim nodes As XmlNodeList = xmldoc.GetElementsByTagName("FNOL")
            For Each node As XmlNode In nodes
                For Each childnode As XmlNode In node.ChildNodes
                    Console.WriteLine(childnode.LocalName & childnode.InnerText)
                Next
            Next

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 2, 2018 3:29 AM