Csharp/C Sharp by API/System.Xml.Linq/XDocument — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:11, 26 мая 2010
Содержание
new 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() {
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 XComment("This is a new author."),
new XProcessingInstruction("AuthorHandler", "new"),
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"))));
foreach (XAttribute attr in firstParticipant.Attributes()) {
Console.WriteLine(attr);
}
}
}
XDocument.Changed
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")),
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "E"),
new XElement("LastName", "B"))));
Console.WriteLine("{0}{1}", xDocument, System.Environment.NewLine);
firstParticipant.Changing += new EventHandler<XObjectChangeEventArgs>(
(object sender, XObjectChangeEventArgs cea) =>
Console.WriteLine("Type of object changing: {0}, Type of change: {1}",
sender.GetType().Name, cea.ObjectChange));
firstParticipant.Changed += (object sender, XObjectChangeEventArgs cea) =>
Console.WriteLine("Type of object changed: {0}, Type of change: {1}",
sender.GetType().Name, cea.ObjectChange);
xDocument.Changed += (object sender, XObjectChangeEventArgs cea) =>
Console.WriteLine("Doc: Type of object changed: {0}, Type of change: {1}{2}",
sender.GetType().Name, cea.ObjectChange, System.Environment.NewLine);
firstParticipant.Element("FirstName").Value = "Seph";
Console.WriteLine("{0}{1}", xDocument, System.Environment.NewLine);
}
}
XDocument.Load
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());
}
}
XDocument.Validate
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
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("MiddleInitial", "C"),
new XElement("LastName", "B")),
new XElement("Book",
new XAttribute("type", "Author"),
new XElement("FirstName", "C"),
new XElement("LastName", "D"))));
Console.WriteLine(xDocument);
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "bookparticipants.xsd");
try {
xDocument.Validate(schemaSet, null);
} catch (XmlSchemaValidationException ex) {
Console.WriteLine("Exception occurred: {0}", ex.Message);
}
}
}
XDocument.XPathSelectElement
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
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"))));
XElement bookParticipant = xDocument.XPathSelectElement("//Books/Book[FirstName="A"]");
Console.WriteLine(bookParticipant);
}
}