Get the XNamespace object that corresponds to no namespace in CSharp

Description

The following code shows how to get the XNamespace object that corresponds to no namespace.

Example


  //from www . ja va2s .  co  m
  
  
   

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

public class MainClass{
   public static void Main(){        
        XNamespace aw = "http://www.domain.com";
        
        XElement root = new XElement("Root",
            new XElement(aw + "ChildInNamespace", "content"),
            new XElement("ChildInNoNamespace", "content")
        );
        
        if (root.Name.Namespace == XNamespace.None)
            Console.WriteLine("Root element is in no namespace");
        else
            Console.WriteLine("Root element is in a namespace");
        
        if (root.Element(aw + "ChildInNamespace").Name.Namespace == XNamespace.None)
            Console.WriteLine("ChildInNamespace element is in no namespace");
        else
            Console.WriteLine("ChildInNamespace element is in a namespace");
        
        if (root.Element("ChildInNoNamespace").Name.Namespace == XNamespace.None)
            Console.WriteLine("ChildInNoNamespace element is in no namespace");
        else
            Console.WriteLine("ChildInNoNamespace element is in a namespace");
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style