Csharp/CSharp Tutorial/XML/Xml Write

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

Read and write with XmlReader and XmlWriter

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
public class MainClass
{
    public static void Main()
    {
        XmlReader reader = XmlReader.Create("pubs.xml");
        XmlWriter writer = XmlWriter.Create("output.xml");
        while (reader.Read())
        {
            writer.WriteNode(reader, true);
        }
        reader.Close();
        writer.Close();
    }
}

Read one xml to write it to another xml

using System;
using System.Xml;
class MainClass
{
  static void Main(string[] args)
  {
    XmlTextWriter writer = new XmlTextWriter("C:\\xmlWriterTest.xml", null);
    writer.WriteStartDocument();
    writer.WriteEndElement();
    XmlTextReader reader = new XmlTextReader(@"C:\node.xml");
        
    while (!reader.EOF)
    {
      writer.WriteNode(reader, false);
    }

    // Ends the document.
    writer.WriteEndDocument();
    writer.Close();
    return;
  }
}

Read/Write Xml document with FileStream

using System;
using System.Xml;
using System.IO;
using System.Text;
public class MainClass 
{
  private static void Main()
  {
    FileStream fs = new FileStream("products.xml", FileMode.Create);
        XmlWriter w = XmlWriter.Create(fs); 
    w.WriteStartDocument();
    w.WriteStartElement("products");
    // Write a product.
    w.WriteStartElement("product");
    w.WriteAttributeString("id", "1001");  
    w.WriteElementString("productName", "Coffee");
    w.WriteElementString("productPrice", "0.99");
    w.WriteEndElement();
    w.WriteEndDocument();
    w.Flush();
    fs.Close();
      
    fs = new FileStream("products.xml", FileMode.Open);
        
        XmlReader r = XmlReader.Create(fs);
    
    while (r.Read())
    {
      if (r.NodeType == XmlNodeType.Element)
      {
        Console.WriteLine();
        Console.WriteLine("<" + r.Name + ">");
        if (r.HasAttributes)
        {
          for (int i = 0; i < r.AttributeCount; i++)
          {
            Console.WriteLine("\tATTRIBUTE: " + r.GetAttribute(i));
                    }
        }
      }
      else if (r.NodeType == XmlNodeType.Text)
      {                    
        Console.WriteLine("\tVALUE: " + r.Value);
      }
    }
  }
}
<products>
<product>
        ATTRIBUTE: 1001
<productName>
        VALUE: Coffee
<productPrice>
        VALUE: 0.99

Save Data As Xml

using System;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
public class SaveDataAsXml {
  public static void Main(string [] args) {
    SqlConnection connection = new SqlConnection("Initial Catalog=Product; Integrated Security=SSPI; User ID=sa");
    string command = "SELECT name FROM customers";
    SqlCommand xmlCommand = new SqlCommand(command, connection);
    connection.Open();
    XmlReader reader = xmlCommand.ExecuteXmlReader();
    XmlDocument doc = new XmlDocument();
    doc.Load(reader);
    doc.Save(Console.Out);
    connection.Close();
  }
}

Write value to Xml

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
    public class MainClass
    {
        public static void Main()
        {
            XmlWriter writer;
            writer = XmlWriter.Create("output.xml");
            writer.WriteStartDocument();
            writer.WriteStartElement("pubs");
            writer.WriteStartElement("titles");
            writer.WriteStartAttribute("name");
            writer.WriteValue("value");
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("price");
            writer.WriteValue(19.99);
            writer.WriteEndAttribute();
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Close();
        }
    }

Write whole xml document as a node

using System;
using System.Xml;
class MainClass
{
  static void Main(string[] args)
  {
    XmlTextWriter writer = new XmlTextWriter("C:\\xmlWriterTest.xml", null);
    writer.WriteStartDocument();
    XmlTextReader reader = new XmlTextReader(@"C:\node.xml");
        
    while (!reader.EOF)
    {
      writer.WriteNode(reader, false);
    }
    writer.WriteEndDocument();
    writer.Close();
    return;
  }
}

Write XML to file

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
class MainClass
{
  static void Main(string[] args)
  {
    XmlTextWriter myXmlTextWriter = null;

    myXmlTextWriter = new XmlTextWriter("books.xml", null);
    myXmlTextWriter.Formatting = Formatting.Indented;
    myXmlTextWriter.WriteStartDocument(false);
    myXmlTextWriter.WriteDocType("bookstore", null, "books.dtd", null);
    myXmlTextWriter.WriteComment("comment");
    myXmlTextWriter.WriteStartElement("bookstore");
    myXmlTextWriter.WriteStartElement("book", null);
    myXmlTextWriter.WriteAttributeString("genre", "autobiography");
    myXmlTextWriter.WriteAttributeString("publicationdate", "1979");
    myXmlTextWriter.WriteAttributeString("ISBN", "0-9999-9999-9");
    myXmlTextWriter.WriteElementString("title", null, "Title");
    myXmlTextWriter.WriteStartElement("Author", null);
    myXmlTextWriter.WriteElementString("first-name", "first");
    myXmlTextWriter.WriteElementString("last-name", "last");
    myXmlTextWriter.WriteEndElement();
    myXmlTextWriter.WriteElementString("price", "799.99");
    myXmlTextWriter.WriteEndElement();
    myXmlTextWriter.WriteEndElement();
    //Write the XML to file and close the writer
    myXmlTextWriter.Flush();
    myXmlTextWriter.Close();
    if (myXmlTextWriter != null)
      myXmlTextWriter.Close();
  }
}