Solved

Pars this xml sample using vb.net

Posted on 2009-05-05
7
516 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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