XAttribute Properties
In this chapter you will learn:
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:
Home » C# Tutorial » XML Linq