TheWizardofInt Posted November 9, 2004 Posted November 9, 2004 I have this format for an XML form: <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 isn't well formed so I can't 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 couldn't see it. I need to read <Individuals-Name>Joe Light</Individuals-Name> and put that into a datatable, where Column1's Name is 'Name', and Column2's 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: 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 � returns False if no more nodes to read Do While objXMLReader.Read() 'select on the type of the node (these are only some of the types) objNodeType = objXMLReader.NodeType Select Case objNodeType 'Case XmlNodeType.XmlDeclaration 'get the name and value ' strNodeResult += "|XML Declaration: " & objXMLReader.Name _ ' & " " & objXMLReader.Value & "|" & Chr(10) Case XmlNodeType.Element 'just get the name, any value will be in next (#text) node 'strNodeResult += "Element: *" & objXMLReader.Name & "*" & Chr(10) sName = objXMLReader.Name If sValue.Length > 0 Then dr = dt.NewRow dr.Item("Value") = sValue dr.Item("Name") = sName dt.Rows.Add(dr) End If sValue = "" sName = "" Case XmlNodeType.Text 'just display the value, node name is "#text" in this case sValue = objXMLReader.Value End Select Loop 'and read the next node 'and return the resulting string Return dt End Function To help me out here? Thanks Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
TheWizardofInt Posted November 10, 2004 Author Posted November 10, 2004 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 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. Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
VBAHole22 Posted June 9, 2005 Posted June 9, 2005 Most helpful. Thank you. Quote Wanna-Be C# Superstar
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.