I have tried Google, but no avail.
I have an XML file, say

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.


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

XmlDocument doc = new XmlDocument();

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

Sorted. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.