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() {
XDocument xDocument = new XDocument(
new XElement("Books",
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "A"),
new XElement("LastName", "B")),
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "C"),
new XElement("LastName", "D"))));
IEnumerable<XElement> elements =
from e in xDocument.Descendants("Book")
where ((string)e.Attribute("type")) != "Illustrator"
orderby ((string)e.Element("LastName"))
select e;
foreach (XElement element in elements) {
Console.WriteLine("Element: {0} : value = {1}",element.Name, element.Value);
}
}
}
XML LINQ
Obtaining Restricted Elements Without Reaching
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() {
XDocument xDocument = new XDocument(
new XElement("Books",
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "A"),
new XElement("LastName", "B")),
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "C"),
new XElement("LastName", "D"))));
IEnumerable<XElement> elements = xDocument.Descendants("Book")
.Where(e => ((string)e.Element("FirstName")) == "C");
foreach (XElement element in elements) {
Console.WriteLine("Element: {0} : value = {1}",element.Name, element.Value);
}
}
}
Obtaining Elements Without Reaching
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() {
XDocument xDocument = new XDocument(
new XElement("Books",
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "A"),
new XElement("LastName", "B")),
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "C"),
new XElement("LastName", "D"))));
IEnumerable<XElement> elements = xDocument.Descendants("Book");
foreach (XElement element in elements) {
Console.WriteLine("Element: {0} : value = {1}",element.Name, element.Value);
}
}
}
Adding a Node to the End of the Specified Node's Child Nodes with Add
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() {
XDocument xDocument = new XDocument(
new XElement("Books",
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
xDocument.Element("Books").Add(
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "E"),
new XElement("LastName", "B")));
Console.WriteLine(xDocument);
}
}
An Example with a Single Book
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() {
XDocument xDocument = new XDocument(
new XElement("Books",
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
Console.WriteLine(xDocument);
}
}
Accessing the XML Document from an XElement Object via the Document 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 XDeclaration("1.0", "UTF-8", "yes"),
new XDocumentType("Books", null, "Books.dtd", null),
new XProcessingInstruction("Book", "out-of-print"),
new XElement("Books", firstParticipant =
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "J"),
new XElement("LastName", "R")),
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "E"),
new XElement("LastName", "B"))));
Console.WriteLine(firstParticipant.Document);
}
}
Is an element empty
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;
class Program {
static void Main(string[] args) {
XElement xml = XElement.Load("Employee.xml");
XElement firstName = xml.Descendants("firstname").First();
Console.WriteLine("Is FirstName tag empty? {0}", firstName.IsEmpty ? "Yes" : "No");
XElement idEmployee = xml.Descendants("idperson").First();
Console.WriteLine("Is idperson tag empty? {0}", idEmployee.IsEmpty ? "Yes" : "No");
}
}