Csharp/CSharp Tutorial/XML/Xml Display

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

Output XmlDocument to Console

<source lang="csharp">using System; using System.IO; using System.Xml; public class MainClass {

 public static void Main()
 {
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.LoadXml("<book genre="programming"> <title>ADO.NET Programming</title> </book>");
   XmlNode root = xmlDoc.DocumentElement;
   Console.WriteLine("XML Document Fragment");
   xmlDoc.Save(Console.Out);
   Console.WriteLine();
   Console.WriteLine("XML Document Fragment After RemoveAll");
   root.RemoveAll();
   xmlDoc.Save(Console.Out);
 }

}</source>

XML Document Fragment
<?xml version="1.0" encoding="gb2312"?>
<book genre="programming">
  ADO.NET Programming</title>
</book>
XML Document Fragment After RemoveAll
<?xml version="1.0" encoding="gb2312"?>
<book>
</book>

Save XML document to console and through XmlTextWriter

<source lang="csharp">using System; using System.Xml; class MainClass {

 static void Main(string[] args)
 {
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.Load(@"C:\xmlWriterTest.xml");
   XmlTextWriter writer = new XmlTextWriter("C:\\domtest.xml", null);
   writer.Formatting = Formatting.Indented;
   xmlDoc.Save(writer);
   xmlDoc.Save(Console.Out);
 }       
  

}</source>