XAttribute namespace

In this chapter you will learn:

  1. Create XAttribute with namespace
  2. Is XAttribute Namespace Declaration

Create XAttribute with namespace

using System;/*j a v  a  2 s  . c o m*/
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(aw + "AnAttributeName", "Content"));
   }
}

XAttribute.IsNamespaceDeclaration

using System;//from   ja v  a2 s.com
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);
        }
   }
}

Next chapter...

What you will learn in the next chapter:

  1. Creating a Comment and Adding It to Its Element
  2. Add a comment to XDocument
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