Solved

Simple XML Parse in Visual Basic.net 2008

Posted on 2008-06-09
5
249 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
Comment Utility
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)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

10 Experts available now in Live!

Get 1:1 Help Now