XNamespace

In this chapter you will learn:

  1. Add an element that uses the namespace

Add an element that uses the namespace

using System;/* j  a  v  a2s.co m*/
using System.Collections.Generic;
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"));

   }
}

Next chapter...

What you will learn in the next chapter:

  1. What is Reflection
Home » C# Tutorial » XML Linq
XDocument
Create XDocument
Add to XDocument
Parse XML file with XDocument
Load XML string with XDocument
XDocument Root
Query XML document with Linq
Save XML document
XDocument Serialize
XElement namespace
Adding attribute
XElement's NextNode
XAttribute
XAttribute value
XAttribute Properties
XAttribute namespace
XComment
XDeclaration
XML declaration
XCData
XNamespace