Solved

Pars this xml sample using vb.net

Posted on 2009-05-05
7
513 Views
Last Modified: 2013-11-11
Fairly new to this process and I need to parse this xml file. Any assistance would be great. I can iterate through each element and grab the data pertaining to it.  Below is the code I have, unfortunately I need to do a few more things such as grabbing the customer id(s) etc.
Dim doc As XmlDocument = New XmlDocument

        doc.Load("C:\XML\2209_SIC_Claims_Extract_Inception_to_Date_March2009.xml")

        Dim m_xmld As XmlDocument

        Dim m_nodelist As XmlNodeList

        Dim m_node As XmlNode

        'Create the XML Document

        m_xmld = New XmlDocument()

        'Load the Xml file

        m_xmld.Load("C:\XML\2209_SIC_Claims_Extract_Inception_to_Date_March2009.xml")

        'Get the list of nodes 

        m_nodelist = m_xmld.SelectNodes("//LossRunInquiryResult")

        'Loop through the nodes

        Dim Count As Integer

        Dim VariableName As String

        Dim VariableValue As String

        Dim nvCollection As New System.Collections.Specialized.NameValueCollection

        For Each m_node In m_nodelist

            For i As Integer = 0 To m_node.ChildNodes.Count - 1

                VariableName = m_node.ChildNodes.Item(i).Name

                VariableValue = m_node.ChildNodes.Item(i).InnerText

                Console.Write(VariableName & ":" & VariableValue)

                Console.Write(vbCrLf)

                Count += 1

                nvCollection.Add(VariableName, VariableValue)

            Next

        Next

Open in new window

2209-SIC-Claims-Extract-Inceptio.txt
0
Comment
Question by:E-Risk
  • 3
  • 2
7 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24310328
I'm not really understand the part where you say "etc"... Do you want more help with the loop to walk the DOM or do you want more help to dig up some SelectNodes XPaths that can give you the information you need? In both cases, I would like to know what "etc" is about ;)

also, I don't see any customer ids in the XML source. What is a customer id?


0
 
LVL 2

Author Comment

by:E-Risk
ID: 24314528
Sorry for not clarifying. Yes I would like to use SelectNodes XPaths that can give me the information I need. As for the customer number I meant ClaimNumber as an example. I am assuming I need to get the ID attribute in order to get these details for the claimnumber. Seems there are varying node types through out the xml file. How would I determine in the loop the node type and grab any details? My ultimate goal is to get the details in the XML file into a database table.

Thanks for your help here and hopefully this is clearer now.
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24341127
If you want to loop through certain nodes and then process the child nodes of those nodes, it is usually best to use a SelectNodes from that child node, which will give you easier control over the objects you are searching.

For Each m_node As XmlNode In m_nodelist
    For Each newNode As XmlNode in m_node.SelectNodes("relative/path/to/node")
    ....

note that this can quickly become very nested. To prevent that, it is usually a good idea to create some functions for the inner nodes that you want to process, but that is a matter of taste.

There are many ways of processing an XML document and I am not sure if this method is the best for you. If you want to change the xml document into another document, XSLT should be chosen. If you want to process it sequentially, XmlTextReader should be used (but be aware, it is tricky and forward-only). More complex processing can be done with XPaths.

An important thing to know when working with DOM and nodes is: they are never disconnected from the rootnode. That means, you can do something like


For Each m_node As XmlNode  In m_nodelist
     id = SelectNodes("../../@id")(0).Value

to get the id attribute of a parent node.

To get information about any node, you can use the .Value, .Name, .LocalName and the many other methods and properties that an XmlNode has.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:E-Risk
ID: 24353918
Thanks Abel,
I will give this a try and let you know how it works. I agree this can be tricky but hopefully this steers me in the correct direction. Thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24522235
0
 
LVL 39

Expert Comment

by:abel
ID: 24730905
I believe my answer in http:#24341127 quite well steers the asker in the right direction. (s)He said to check back after trying. No news is good news?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DataGridview Currency Formating? 22 39
Round up to 100% in .NET 10 45
Create XML 5 34
Post xml via PHP Soap Client 3 10
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now