using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XElement firstParticipant;
XDocument xDocument = new XDocument(
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XAttribute("experience", "first-time"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
Console.WriteLine(xDocument);
firstParticipant.SetAttributeValue("type", "beginner");
firstParticipant.SetAttributeValue("language", "English");
firstParticipant.SetAttributeValue("experience", null);
Console.WriteLine(xDocument);
}
}
XML LINQ
Changing an Attribute's Value
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XElement firstParticipant;
XDocument xDocument = new XDocument(
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XAttribute("experience", "first-time"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
Console.WriteLine(xDocument);
firstParticipant.Attribute("experience").Value = "beginner";
Console.WriteLine(xDocument);
}
}
Removing All of an Element's Attributes
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XElement firstParticipant;
XDocument xDocument = new XDocument(
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XAttribute("experience", "first-time"),
new XElement("FirstName", "A"),
new XElement("LastName", "B"))));
Console.WriteLine(xDocument);
firstParticipant.Attributes().Remove();
Console.WriteLine(xDocument);
}
}
Removing an Attribute
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XElement firstParticipant;
XDocument xDocument = new XDocument(
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
Console.WriteLine(xDocument);
firstParticipant.Attribute("type").Remove();
Console.WriteLine(xDocument);
}
}
Accessing All of an Element's Attributes with the Attributes Method
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XElement firstParticipant;
XDocument xDocument = new XDocument(
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XAttribute("experience", "first-time"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
foreach (XAttribute attr in firstParticipant.Attributes()) {
Console.WriteLine(attr);
}
}
}
Accessing an Attribute with the Attribute Method
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XElement firstParticipant;
XDocument xDocument = new XDocument(
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
Console.WriteLine(firstParticipant.Attribute("type").Value);
}
}
Accessing the Last Attribute with the LastAttribute Property
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
XElement firstParticipant;
XDocument xDocument = new XDocument(
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XAttribute("experience", "first-time"),
new XAttribute("language", "English"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
Console.WriteLine(firstParticipant.LastAttribute);
}
}