Csharp/C Sharp/XML LINQ/XDocument — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:34, 26 мая 2010
Содержание
- 1 Create XDocument from XmlReader
- 2 Creating an XML Document with XDocument
- 3 Get Encoding, Version and standalone from XML declaration
- 4 Intentionally Exposing the Halloween Problem
- 5 Loading a Document with the XDocument.Load Method with LoadOptions
- 6 Query an XML document with Linq
- 7 Saving a Document with the XDocument.Save Method
- 8 Traversing Forward from an XElement Object via the NextNode Property
- 9 Using the LINQ to XML API to Create an XML Document and Adding Some Structure
Create XDocument from XmlReader
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
class Program {
static void Main(string[] args) {
XmlReader reader = XmlReader.Create("Employee.xml");
XDocument xml = XDocument.Load(reader);
Console.WriteLine(xml);
XElement idperson = xml.Descendants("idP").Last();
idperson.Add(new XElement("idperson",
new XAttribute("id", 1),
new XAttribute("year", 2006),
new XAttribute("salary", "16")));
StringWriter sw = new StringWriter();
XmlWriter w = XmlWriter.Create(sw);
xml.Save(w);
w.Close();
Console.WriteLine(sw.ToString());
}
}
Creating an XML Document with XDocument
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 XDeclaration("1.0", "UTF-8", "yes"),
new XDocumentType("Books", null, "Books.dtd", null),
new XProcessingInstruction("Book", "out-of-print"),
new XElement("Books"));
Console.WriteLine(xDocument);
}
}
Get Encoding, Version and standalone from XML declaration
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) {
XDocument xml = XDocument.Load("Hello_XLINQ.xml");
Console.WriteLine("Encoding: {0}", xml.Declaration.Encoding);
Console.WriteLine("Version: {0}", xml.Declaration.Version);
Console.WriteLine("Standalone: {0}", xml.Declaration.Standalone);
}
}
Intentionally Exposing the Halloween Problem
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")),
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "E"),
new XElement("LastName", "B"))));
IEnumerable<XElement> elements =
xDocument.Element("Books").Elements("Book");
foreach (XElement element in elements) {
Console.WriteLine("Source element: {0} : value= {1}",element.Name, element.Value);
}
foreach (XElement element in elements) {
Console.WriteLine("Removing {0}= {1} ¡�", element.Name, element.Value);
element.Remove();
}
Console.WriteLine(xDocument);
}
}
Loading a Document with the XDocument.Load Method with LoadOptions
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 = XDocument.Load("book.xml",LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
Console.WriteLine(xDocument);
XElement firstName = xDocument.Descendants("FirstName").First();
Console.WriteLine("FirstName Line:{0} Position:{1}",((IXmlLineInfo)firstName).LineNumber,((IXmlLineInfo)firstName).LinePosition);
Console.WriteLine("FirstName Base URI:{0}", firstName.BaseUri);
}
}
Query an XML document with Linq
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) {
XDocument xml = XDocument.Load(@"Employee.xml");
var query = from p in xml.Elements("people").Elements("person")
where (int)p.Element("id") == 1
select p;
foreach (var record in query) {
Console.WriteLine("Employee: {0} {1}",
record.Element("firstname").Value,
record.Element("lastname").Value);
}
}
}
Saving a Document with the XDocument.Save 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() {
XDocument xDocument = new XDocument(
new XElement("Books",
new XElement("Book",
new XAttribute("type", "Author"),
new XAttribute("experience", "first-time"),
new XAttribute("language", "English"),
new XElement("FirstName", "J"),
new XElement("LastName", "R"))));
xDocument.Save("book.xml");
}
}
Traversing Forward from an XElement Object via the NextNode 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.NextNode);
}
}
Using the LINQ to XML API to Create an XML Document and Adding Some Structure
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.ToString());
}
}