0

Hey,
I have tried Google, but no avail.
I have an XML file, say
<Items>
<item>
<id>1</id>
<name>test</name>
</item>
<item>
<id>2</id>
<name>othertest</name>
</item>
</Items>

I have a function Add(int id, string name). How would I read through every <item>, get the id and name, and call the add function.

Thanks

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by bbman
0

Import "System.Xml" namespace first: using System.Xml; And use this code to get the item name/id:

XmlDocument doc = new XmlDocument();
            doc.Load("C:\\xml.txt");

            XmlNodeList list = doc.GetElementsByTagName("item");

            int id = 0; 
            string name = string.Empty;

            foreach (XmlNode node in list)
            {
                id = int.Parse(node["id"].InnerText);
                name = node["name"].InnerText;

                Add(id, name);
            }

Edited by farooqaaa: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.