Solved

Simple XML Parse in Visual Basic.net 2008

Posted on 2008-06-09
5
258 Views
Last Modified: 2013-11-26
Visual Studio 2008
Visual Basic.net
Windows Exe Application

I have a need to parse 1000 entries from an xml file daily.
I can't seem to find an example that I can modify that I do not have to go out and learn all the code.

I simply need to parse:


<Item>
<Title>Product 1</Title>
<Description>
<Category>Main</Category>
<Color>Red</Color>
<Type> Flag<Type>
<Text>A Nice red Flag></Text>
</Description>
<Date>02/20/2008</Date>
</Item>

and store each value set in a database.
I can easily store them in the database, but i cannot seem to get anything working to parse the XML
I think my issues start with the ?subnode?  I really do not want to fill my brain with this info as I will probably not use it again. I just need something I can modify and use and then get back to my daily duties.



0
Comment
Question by:EGormly
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:AUmidh
ID: 21748768
Sample xml file
<?xml version="1.0" encoding="utf-8" ?>

 <Items>

 <Item>

<Title>Product 1</Title>

 <Description>

<Category>Main</Category>

<Color>Red</Color>

<Type>Flag</Type>

<Text>A Nice red Flag</Text>

</Description>

<Date>02/20/2008</Date>

</Item>

</Items>

sample code Check out this code this will parse your xml file each node. change accordingly your problem
private void ParseXML()

        {

            XmlDocument doc = new XmlDocument();

            doc.Load("C:\\text1.xml");

            XmlElement rootElement = doc.DocumentElement;

            foreach (XmlNode nd in rootElement.ChildNodes)

            {

                if (nd.ChildNodes.Count>0)

                    ParseSubNodes(nd);

            }

        }

        private void ParseSubNodes(XmlNode parentNode)

        {

            foreach (XmlNode cNode in parentNode.ChildNodes)

            {

                if (cNode.ChildNodes.Count == 1)

                {

                    if (cNode.ChildNodes[0].NodeType == XmlNodeType.Element)

                        ParseSubNodes(cNode);

                    else

                    {

                        // MessageBox.Show(cNode.Name + "     " + cNode.InnerText);

                         // cNode.Name;
                         // cNode.Value;
                         // cNode.InnerText;    
                    }

                }

                else if(cNode.ChildNodes.Count>1)

                    ParseSubNodes(cNode);

            }

        }
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 21749186
Take a look at the DataSet.ReadXML method -> http://msdn.microsoft.com/en-us/library/360dye2a.aspx

It provides an easy way to read an XML file into a DataSet, and from there it should be fairly simple to insert into a database.

Wayne
0
 

Author Comment

by:EGormly
ID: 21750477
AUmidh:
Thank you for the response
That looks promising but it also looks like C#?
I am coding in Visual Basic.net and I don;'t know what to do with that.

Your solution seems like it would work if it were only VB




webtubbs:
Thank you for the response
That also looked promising until I really looked at the code and ran it.
All that does it create an XML file with a table structure.  
It doesn't parse an xml file, it does read it "into" the table, but I think it is looking for that exact structure?

I really don't know how to modify that.
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 21751184
Don't worry about the WriteXML method. You won't need that. Simply use the ReadXML method, specifying the path of the XML file. It will create a DataSet with tables, as per your xml structure, ie, if there are many <Item>....</Item> nodes, the dataset will contain a table called "Item", with each of the nodes within <Item>....</Item> being the Columns names.

Wayne
0
 

Author Comment

by:EGormly
ID: 21751968
Wayne

Thank you.
I believe I will be able to parse from that.. looks a bit tough, but I think I'll get it now.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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