Csharp/CSharp Tutorial/XML/Xml Creation — различия между версиями

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

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

Adding an element to the document

using System;
using System.Xml;
class MainClass
{
  static void Main(string[] args)
  {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml("<Record> Some Value </Record>");
        XmlNode node1 = xmlDoc.CreateComment("DOM Testing Sample");
    xmlDoc.AppendChild( node1);
    node1 = xmlDoc.CreateElement("FirstName");
    node1.InnerText = "M";
    xmlDoc.DocumentElement.AppendChild(node1);
    xmlDoc.Save(Console.Out);
  }    
}
<?xml version="1.0" encoding="gb2312"?>
<Record> Some Value <FirstName>M</FirstName></Record>
<!--DOM Testing Sample-->

Create XML document by specifying the elements

using System;
using System.Collections;
using System.Data;
using System.Xml;
public class MainClass {
   public static void Main() {
      XmlDocument doc = new XmlDocument();
      XmlElement root = doc.CreateElement( "books" );
      doc.AppendChild( root );
      XmlElement eltBook = doc.CreateElement( "book" );
      root.AppendChild( eltBook );
      XmlElement eltTitle = doc.CreateElement( "title" );
      eltTitle.AppendChild( doc.CreateTextNode( "myTitle" ) );
      eltBook.AppendChild( eltTitle );
      XmlElement eltAuthor = doc.CreateElement( "author" );
      eltAuthor.AppendChild( doc.CreateTextNode(  "myAuthor" ) );
      eltBook.AppendChild( eltAuthor );
      XmlElement eltPrice = doc.CreateElement( "price" );
      eltPrice.AppendChild( doc.CreateTextNode(  "10.0" ) );
      eltBook.AppendChild( eltPrice );
      Console.WriteLine( doc.OuterXml );
   }
}
<books><book>myTitle</title><author>myAuthor</author><price>10.0</price></book></books>

Generate Xml Document

using System;
using System.Xml;
public class MainClass
{
  [STAThread]
  private static void Main(string[] args)
  {
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
    doc.AppendChild(docNode);
    XmlNode productsNode = doc.CreateElement("products");
    doc.AppendChild(productsNode);
    XmlNode productNode = doc.CreateElement("product");
    XmlAttribute productAttribute = doc.CreateAttribute("id");
    productAttribute.Value = "1001";
    productNode.Attributes.Append(productAttribute);
        productsNode.AppendChild(productNode);
    XmlNode nameNode = doc.CreateElement("productName");
    nameNode.AppendChild(doc.CreateTextNode("Gourmet Coffee"));
    productNode.AppendChild(nameNode);
    XmlNode priceNode = doc.CreateElement("productPrice");
    priceNode.AppendChild(doc.CreateTextNode("0.99"));
    productNode.AppendChild(priceNode);

      // Save the document (to the Console window rather than a file).
    doc.Save(Console.Out);
  }
}
<?xml version="1.0" encoding="gb2312"?>
<products>
  <product id="1001">
    <productName>Gourmet Coffee</productName>
    <productPrice>0.99</productPrice>
  </product>
</products>

Generate XML Document with attributes

using System;
using System.Xml;
public class MainClass
{
  [STAThread]
  private static void Main(string[] args)
  {
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
    doc.AppendChild(docNode);
    XmlNode productsNode = doc.CreateElement("products");
    doc.AppendChild(productsNode);
    XmlNode productNode = doc.CreateElement("product");
    XmlAttribute productAttribute = doc.CreateAttribute("id");
    productAttribute.Value = "1001";
    productNode.Attributes.Append(productAttribute);
        productsNode.AppendChild(productNode);
    productNode = doc.CreateElement("product");
    productAttribute = doc.CreateAttribute("id");
    productAttribute.Value = "1002";
    productNode.Attributes.Append(productAttribute);
    productsNode.AppendChild(productNode);
    XmlNode nameNode = doc.CreateElement("productName");
    nameNode.AppendChild(doc.CreateTextNode("Pot"));
    productNode.AppendChild(nameNode);
    XmlNode priceNode = doc.CreateElement("productPrice");
    priceNode.AppendChild(doc.CreateTextNode("102.99"));
    productNode.AppendChild(priceNode);

      // Save the document (to the Console window rather than a file).
    doc.Save(Console.Out);
  }
}
<?xml version="1.0" encoding="gb2312"?>
<products>
  <product id="1001" />
  <product id="1002">
    <productName>Pot</productName>
    <productPrice>102.99</productPrice>
  </product>
</products>

Helper method for generating the Xml document

using System;
using System.Xml;
public class MainClass
{
  [STAThread]
  private static void Main(string[] args)
  {
    // Create the basic document.
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
    doc.AppendChild(docNode);
    XmlNode productsNode = doc.CreateElement("products");
    doc.AppendChild(productsNode);
    // Add two products.
    XmlNode productNode = AddElement("product", null, productsNode);
    AddAttribute("id", "1001", productNode);
    AddElement("productName", "Coffee", productNode);
    AddElement("productPrice", "0.99", productNode);
    
    doc.Save(Console.Out);
  }
  public static XmlNode AddElement(string tagName, string textContent, XmlNode parent)
  {
    XmlNode node = parent.OwnerDocument.CreateElement(tagName);
    parent.AppendChild(node);
    if (textContent != null)
    {
      XmlNode content = parent.OwnerDocument.CreateTextNode(textContent);
      node.AppendChild(content);
    }
    return node;
  }
  public static XmlNode AddAttribute(string attributeName, string textContent, XmlNode parent)
  {
    XmlAttribute attribute = parent.OwnerDocument.CreateAttribute(attributeName);
    attribute.Value = textContent;
    parent.Attributes.Append(attribute);
    return attribute;
  }
}
<?xml version="1.0" encoding="gb2312"?>
<products>
  <product id="1001">
    <productName>Coffee</productName>
    <productPrice>0.99</productPrice>
  </product>
</products>