Find Specific Elements by Name - CSharp XML

CSharp examples for XML:Element

Description

Find Specific Elements by Name

Demo Code


using System;//from w w w  .  java 2 s.c  o m
using System.Xml;

public class MainClass
{
    [STAThread]
    private static void Main()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(@"ProductCatalog.xml");

        XmlNodeList prices = doc.GetElementsByTagName("productPrice");

        decimal totalPrice = 0;
        foreach (XmlNode price in prices)
        {
            totalPrice += Decimal.Parse(price.ChildNodes[0].Value);
        }

        Console.WriteLine("Total catalog value: " + totalPrice.ToString());

    }
}

Related Tutorials