Csharp/C Sharp/XML LINQ/XProcessingInstruction — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Adding Processing Instructions After the Document and Element Have Been Constructed

 
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 XElement("FirstName", "J"),
                 new XElement("LastName", "R"))));
        XProcessingInstruction xPI1 = new XProcessingInstruction("Book", "out-of-print"); xDocument.AddFirst(xPI1);
        XProcessingInstruction xPI2 = new XProcessingInstruction("ParticipantDeleter",
          "delete"); XElement outOfPrintParticipant = xDocument.Element("Books")
          .Elements("Book")
          .Where(e => ((string)((XElement)e).Element("FirstName")) == "J"
          && ((string)((XElement)e).Element("LastName")) == "R")
          .Single<XElement>();
        outOfPrintParticipant.AddFirst(xPI2);
        Console.WriteLine(xDocument);
    }
}


Creating a Processing Instruction at Both the Document and Element Levels

 
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 XProcessingInstruction("Book", "out-of-print"),
          new XElement("Books",
          new XElement("BookParticipant",
         new XProcessingInstruction("ParticipantDeleter", "delete"),
          new XElement("FirstName", "J"),
          new XElement("LastName", "R"))));
        Console.WriteLine(xDocument);
    }
}


Use XProcessingInstruction

 
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) {
        XNamespace w = "http://schemas.microsoft.ru/office/word/2003/wordml";
        XDocument word = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                            new XProcessingInstruction("mso-application", "progid=\"Word.Document\""),
                                            new XElement(w + "wordDocument",
                                                new XAttribute(XNamespace.Xmlns + "w", w.NamespaceName)));
        System.IO.StringWriter sw = new System.IO.StringWriter();
        word.Save(sw);
        Console.WriteLine(sw);
    }
}