Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple XML Parse in Visual Basic.net 2008

Posted on 2008-06-09
5
Medium Priority
?
271 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
[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
  • 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 2000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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