Solved

Simple XML Parse in Visual Basic.net 2008

Posted on 2008-06-09
5
255 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

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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