简单xml示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string dataxml = @"
<Products>
<Product>
<ProductID>1</ProductID>
<ProductName>LINQ to XML</ProductName>
<UnitPrice>10</UnitPrice>
</Product>
<Product>
<ProductID>2</ProductID>
<ProductName>LINQ to SQL</ProductName>
<UnitPrice>20</UnitPrice>
</Product>
</Products>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(dataxml);
            XmlElement root=doc.DocumentElement;
            XmlNodeList objxml = root.SelectNodes("/Products/Product");
            foreach (XmlNode e in objxml)
            {
                Console.WriteLine("{0}-{1}-{2}", e.SelectSingleNode("ProductID").InnerText, e.SelectSingleNode("ProductName").InnerText, e.SelectSingleNode("UnitPrice").InnerText);               
            }
            Console.ReadLine();
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/myloveblogs/p/5582953.html