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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.