Mega Code Archive

 
Categories / C# Book / 05 LINQ XML
 

0544 XElement and namespace

When you construct a child XElement, you must give it a namespace explicitly if needed using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Text; using System.IO; class Program { static void Main() { XNamespace ns = "http://domain.com/xmlspace"; var data = new XElement(ns + "data", new XElement(ns + "customer", "Bloggs"), new XElement(ns + "purchase", "Bicycle") ); Console.WriteLine(data); } } The output: BloggsBicycle