Add an element that uses the namespace in CSharp

Description

The following code shows how to add an element that uses the namespace.

Example


/*from   w  ww  . j  a  v a 2 s .  co  m*/
using System;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
      XDocument NewDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Root", "MyDoc"));

      XNamespace MyNS = "http://www.java2s.com/";

      // Add the namespace to the root node.
      NewDoc.Element("Root").Name = MyNS.GetName("Root");

      // Add an element that uses the namespace.
      NewDoc.Element(MyNS + "Root").Add(
         new XElement(MyNS + "Child1", "Some Data1 "),
         new XElement(MyNS + "Child2", new XAttribute(XNamespace.Xmlns + "NewNS", "http://www.java2s.com"),"Some Data 2"));

      Console.WriteLine(NewDoc);
   }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style