XAttribute Properties

In this chapter you will learn:

  1. XAttribute.PreviousAttribute
  2. XAttribute.NextAttribute
  3. XAttribute.Name

XAttribute.PreviousAttribute

XAttribute.PreviousAttribute Property returns the previous attribute of the parent element.

using System;//from   ja  v  a2 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(){
        XElement root = new XElement("Root",
            new XAttribute("Att1", 1),
            new XAttribute("Att2", 2),
            new XAttribute("Att3", 3),
            new XAttribute("Att4", 4)
        );
        XAttribute att = root.LastAttribute;
        do {
            Console.WriteLine(att);
        }
        while((att = att.PreviousAttribute) != null);
    }
}

XAttribute.NextAttribute

XAttribute.NextAttribute Property returns the next attribute of the parent element.

using System;/*from   j  a  va 2s  .co  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(){
        XElement root = new XElement("Root",
            new XAttribute("Att1", 1),
            new XAttribute("Att2", 2),
            new XAttribute("Att3", 3),
            new XAttribute("Att4", 4)
        );
        
        XAttribute att = root.FirstAttribute;
        do {
            Console.WriteLine(att);
        }
        while((att = att.NextAttribute) != null);
   }
}

XAttribute.Name

XAttribute.Name Property returns the expanded name of this attribute.

using System;/*from   ja v  a 2  s. c  om*/
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"),
            new XAttribute("Att2", "different content")
        );
        
        foreach (XAttribute att in root.Attributes())
            Console.WriteLine("{0}={1}", att.Name, att.Value);
        
        XElement newRoot = new XElement(aw + "Root",
            from att in root.Attributes("Att2")
            select new XAttribute(att.Name, "new content"));
        
        foreach (XAttribute att in newRoot.Attributes())
            Console.WriteLine("{0}={1}", att.Name, att.Value);
}}

Next chapter...

What you will learn in the next chapter:

  1. Create XAttribute with namespace
  2. Is XAttribute Namespace Declaration
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