Reading from .XML

TheWizardofInt

Well-known member
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
I have this format for an XML form:

Code:
<Individuals>
- <Individual>
  <Individuals-Name>Joe Light</Individuals-Name> 
  <Individuals-ID-Number>3999</Individuals-ID-Number> 
  <Main-Applicants-Name>Joe P Light</Main-Applicants-Name> 
  <Main-Applicants-ID-Number>3999</Main-Applicants-ID-Number> 
  <Relationship-to-Main-Applicant>Self</Relationship-to-Main-Applicant> 
  <Birthdate>03-05-1973</Birthdate> 
  <Gender>Male</Gender> 
  <Smoker>Smoker</Smoker> 
- <Application-Data>
  <Rate>163.85</Rate> 
  <PlanCode /> 
  <PlanNumber>MHP007</PlanNumber> 
  </Application-Data>
- <Dependent-Data>
  <SSN- /> 
  <depHeight /> 
  <depWeight /> 
  </Dependent-Data>
  </Individual>
</Individuals>

It is a lot longer than that, and the values within it will tend to vary. It isnt well formed so I cant just import it to a dataset.

So I am stuck manually parsing this thing using XML functions - totally new ground for me.

Seems all of the examples want to tell me how to return the whole thing, as if I were blind and couldnt see it.

I need to read <Individuals-Name>Joe Light</Individuals-Name> and put that into a datatable, where Column1s Name is Name, and Column2s Name is Value, entering the name of the node I am getting (Individuals-Name) and its value (Joe Light) respectively

I have been able to read elements and tell that they have no values and what not - but does anyone have a snippet like this one:

Code:
Function ParseXmlContent(ByVal objXMLReader As XmlTextReader) As DataTable

        read or "pull" the nodes of the XML document   
        Dim strNodeResult As String = ""
        Dim sName As String = ""
        Dim sValue As String = ""
        Dim objNodeType As XmlNodeType
        Dim dt As New DataTable
        Dim dr As DataRow

        dt.Columns.Add("Name")
        dt.Columns.Add("Value")
        read each node in turn
 
Actually solved this for myself

XML takes some getting of your mind around, it seems, so I am going to post the code I used for the next guy with the same question

Code:
Dim oXML As New XmlDocument
        Dim oFil As New FileStream(_FileName, FileMode.Open)
        Dim oNodeList As XmlNodeList
        Dim oNode As XmlNode
        Dim sName As String = ""
        Dim sValue As String = ""
        Dim sNode As String = ""
        Dim dr As DataRow
        Dim dt As New DataTable
        Dim GlobDS As DataSet
        dim globCount As Integer

        dt.Columns.Add("Node")
        dt.Columns.Add("Name")
        dt.Columns.Add("Value")

        oXML.Load(oFil)

        oFil.Close()

        oNodeList = oXML.GetElementsByTagName("*")

        For Each oNode In oNodeList
            sName = oNode.Name
here, you can see that I am looking for the first instance of a 
new node called "Individuals-Name".  This lets me create a new
datatable for each contact in the XML stream
            If sName = "Individuals-Name" And dt.Rows.Count > 0 Then
                globDS.Tables.Add(dt)
                dt = New DataTable
                dt.Columns.Add("Node")
                dt.Columns.Add("Name")
                dt.Columns.Add("Value")
                globCount = globCount + 1
I am creating a new datatable for every contact set in the XML stream
I am counting them because I am going to have a datagrid display the tables
one-at-a-time, and I want to page through them
            End If
            If oNode.HasChildNodes Then
what I am counting on here is the format of <node><child><value>
If you have been playing with this, you have noticed that the node 
value is a child of the node name, but where nodes are grouped by 
descriptors like Personal Data and such, the descriptor creates a new
subset.  Now I can, at a glance or programatically, look at the Node 
field and see what part of the whole set the node belongs to
                sValue = oNode.ChildNodes(0).Value
                If Not sValue = Nothing Then
                    If sValue.Length > 0 Then
                        dr = dt.NewRow
                        dr.Item("Value") = sValue
                        dr.Item("Name") = sName
                        dr.Item("Node") = sNode
                        dt.Rows.Add(dr)
                    End If
                    sValue = ""
                    sName = ""
                Else
                    handle creating a new node
                    sNode = oNode.Name
                End If
            End If
        Next
        globCount = 0
now I display the results in a datagrid, and I can do anything I want with it
later, I will put an Up and Down button on the form so that I can move from
contact set to contact set
        With dgDisplay
            .DataSource = globDS.Tables(globCount)
            .Refresh()
        End With

I hope this helps the next person behind me on the learning curve. It was quite a climb to get here.
 
Back
Top