XML Fragments : XElement « XML LINQ « C# / CSharp Tutorial






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


    class Program
    {
        static void Main(string[] args)
        {
            XElement xcust = 
                new XElement("customers",
                    new XElement("customer",
                        new XAttribute("ID", "A"),
                        new XAttribute("City", "New York"),
                        new XAttribute("Region", "North America"),
                        new XElement("order",
                            new XAttribute("Item", "Widget"),
                            new XAttribute("Price", 100)
                      ),
                      new XElement("order",
                        new XAttribute("Item", "Tire"),
                        new XAttribute("Price", 200)
                      )
                    )
                );
            string xmlFileName = "e.xml";
            xcust.Save(xmlFileName);
            XElement xcust2 = XElement.Load(xmlFileName);
            Console.WriteLine(xcust);
        }
    }








31.2.XElement
31.2.1.Parse Load xml from hard coded string
31.2.2.XML Fragments
31.2.3.Get Descendants for a node
31.2.4.Deal with White Spaces
31.2.5.Load xml from string, text file
31.2.6.Remove an element