Solved

Pars this xml sample using vb.net

Posted on 2009-05-05
7
519 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

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, …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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