Link to home
Start Free TrialLog in
Avatar of EGormly
EGormlyFlag for United States of America

asked on

Simple XML Parse in Visual Basic.net 2008

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.



Avatar of AUmidh
AUmidh

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);

            }

        }
Avatar of Wayne Taylor (webtubbs)
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
Avatar of EGormly

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EGormly

ASKER

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.