Solved

Pars this xml sample using vb.net

Posted on 2009-05-05
7
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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