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

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

Текущая версия на 11:34, 26 мая 2010

Updating a Node"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 XComment("This is a new author."),
              new XAttribute("type", "Author"),
              new XElement("FirstName", "J"),
              new XElement("LastName", "R"))));
        Console.WriteLine(xDocument);
        firstParticipant.Element("FirstName").Value = "J";
        firstParticipant.Nodes().OfType<XComment>().Single().Value ="LINQ";
        ((XElement)firstParticipant.Element("FirstName").NextNode).Nodes().OfType<XText>().Single().Value = "R";
        Console.WriteLine(xDocument);
    }
}


Updating a Processing Instruction

 
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() {
        XProcessingInstruction procInst;
        XDocument xDocument = new XDocument(
          new XElement("Books"),
          procInst = new XProcessingInstruction("Book", "out-of-print"));
        Console.WriteLine(xDocument);
        procInst.Target = "BookContactManager";
        procInst.Data = "update";
        Console.WriteLine(xDocument);
    }
}


Updating the Document Type

 
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() {
        XDocumentType docType;
        XDocument xDocument = new XDocument(docType = new XDocumentType("Books", null,"Books.dtd", null),
          new XElement("Books"));
        Console.WriteLine("Before updating document type:");
        Console.WriteLine(xDocument);
        docType.Name = "MyBooks";
        docType.SystemId = "http://www.yoursite.ru/DTDs/MyBooks.DTD";
        docType.PublicId = "-//DTDs//TEXT Book Participants//EN";
        Console.WriteLine(xDocument);
    }
}