Check if attribute has a namespace declaration in CSharp

Description

The following code shows how to check if attribute has a namespace declaration.

Example


   // w w w  .  j  av a2s .  c o m
using System;
using System.Linq;
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(aw + "Root",
            new XAttribute(XNamespace.Xmlns + "aw", "http://www.domain.com"),
            new XAttribute(aw + "Att", "content")
        );
        
        foreach (XAttribute att in root.Attributes()) {
            if (att.IsNamespaceDeclaration)
                Console.WriteLine("{0} is a namespace declaration", att.Name);
            else
                Console.WriteLine("{0} is not a namespace declaration", att.Name);
        }
   }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style