Csharp/C Sharp by API/System.Xml/XmlDocument — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
| |
Версия 15:31, 26 мая 2010
Содержание
new XmlDocument()
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 );
}
}
XmlDocument.AppendChild(XmlNode docNode)
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;
}
}
XmlDocument.CreateComment(String comments)
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);
}
}
XmlDocument.CreateElement
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);
}
}
XmlDocument.CreateXmlDeclaration
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);
}
}
XmlDocument.LoadXml(String xmlString)
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);
}
}
XmlDocument.Save()
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);
}
}
XmlDocument.SelectNodes
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);
}
}
}
XmlDocument.SelectSingleNode
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( );
}
}