Csharp/C Sharp/XML LINQ/XDeclaration

Материал из .Net Framework эксперт
Версия от 14:33, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Creating a Declaration

<source lang="csharp"> 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 XElement("BookParticipant"));
       Console.WriteLine(xDocument);
   }

}

</source>


Creating a Declaration and Setting the Document"s Declaration Property to It

<source lang="csharp"> 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("BookParticipant"));
       XDeclaration xDeclaration = new XDeclaration("1.0", "UTF-8", "yes"); xDocument.Declaration = xDeclaration;
       Console.WriteLine(xDocument);
   }

}

</source>


Use XDeclaration,XElement, XAttribute to create an XDocument

<source lang="csharp"> 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 = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
                                       new XElement("people",
                                           new XElement("idperson",
                                           new XAttribute("id", 1),
                                           new XAttribute("year", 2004),
                                           new XAttribute("salary", "100"))));
       System.IO.StringWriter sw = new System.IO.StringWriter();
       xml.Save(sw);
       Console.WriteLine(sw);
   }

}

</source>