Csharp/C Sharp by API/System.Xml/XmlDocument

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

new XmlDocument()

<source lang="csharp"> 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 );
  }

}


 </source>


XmlDocument.AppendChild(XmlNode docNode)

<source lang="csharp">


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;
 }

}


 </source>


XmlDocument.CreateComment(String comments)

<source lang="csharp"> 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);
 }    

}


 </source>


XmlDocument.CreateElement

<source lang="csharp">

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);
 }

}


 </source>


XmlDocument.CreateXmlDeclaration

<source lang="csharp"> 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);
 }

}


 </source>


XmlDocument.LoadXml(String xmlString)

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

 static void Main(string[] args)
 {
   XmlDocument xmlDoc = new XmlDocument();    
   xmlDoc.LoadXml("<Record> write something</Record>");
       xmlDoc.Save(Console.Out);
 }    

}


 </source>


XmlDocument.Save()

<source lang="csharp"> 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);
 }    

}


 </source>


XmlDocument.SelectNodes

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

   private static void Main() {
       // Load the document.
       XmlDocument doc = new XmlDocument();
       doc.Load("books.xml");
       XmlNodeList nodes = doc.SelectNodes("/books/A/B");
           
       foreach (XmlNode node in nodes) {
           Console.WriteLine(node.InnerText);
       }
   }

}

 </source>


XmlDocument.SelectSingleNode

<source lang="csharp"> using System; using System.Xml; using System.Xml.XPath; public class XPathQuery {

 public static void Main(string [] args) {
   string filename = "inventory.xml";
   string xpathExpression = "//inventory/items";
   XmlDocument document = new XmlDocument( );
   document.Load(filename);
   XmlTextWriter writer = new XmlTextWriter(Console.Out);
   writer.Formatting = Formatting.Indented;
   XmlNode node = document.SelectSingleNode(xpathExpression);
   node.WriteTo(writer);
   writer.Close( );
 }

}

 </source>