C#操作Xml全集(1)
来源:互联网 发布:淘宝上新率 编辑:程序博客网 时间:2024/04/30 23:28
http://blog.csdn.net/lg836/archive/2007/06/30/1672476.aspx
public class ManagementXml
...{
public ManagementXml()
...{
}
/**//// <summary>
/// 添加元素
/// </summary>
public void AddElement()
...{
System.Xml.XmlNode newElement;
newElement = xml.CreateElement("Keyword");
newElement.InnerText = "Value";
xml.AppendChild(newElement);
}
/**//// <summary>
/// 列举所有属性
/// </summary>
public void EnumAttribute()
...{
foreach (System.Xml.XmlAttribute attribute in node.Attributes)
...{
}
}
/**//// <summary>
/// 查到节点名
/// </summary>
public void Find_A_Node()
...{
System.Xml.XmlNodeList nodes;
nodes = xml.GetElementsByTagName("ElementName");
}
/**//// <summary>
/// 从Xml文件中提出模式
/// </summary>
public void Infer_A_Schema_From_A_Xml()
...{
// Gets the schema.
System.Xml.Schema.XmlSchemaInference infer = new System.Xml.Schema.XmlSchemaInference();
System.Xml.Schema.XmlSchemaSet sc = new System.Xml.Schema.XmlSchemaSet();
sc = infer.InferSchema(new System.Xml.XmlTextReader("sample.xml"));
// Writes the schema.
System.Xml.XmlWriter w = System.Xml.XmlWriter.Create(new System.IO.StreamWriter("sampleSchema.xsd"));
foreach (System.Xml.Schema.XmlSchema schema in sc.Schemas())
...{
schema.Write(w);
}
// Update the schema with another xml fragment.
sc = infer.InferSchema(System.Xml.XmlReader.Create("anothersample.xml"), sc);
w = System.Xml.XmlWriter.Create(new System.IO.StreamWriter("anotherschema.xsd"));
foreach (System.Xml.Schema.XmlSchema schema in sc.Schemas())
...{
schema.Write(w);
}
}
/**//// <summary>
/// 重新声明已命名节点
/// </summary>
public void Iterate_Named_Nodes()
...{
foreach (System.Xml.XmlNode node in xmlDoc.GetElementsByTagName("ElementName"))
...{
}
}
/**//// <summary>
/// 用XPathNavigator导航数据
/// </summary>
public void NavigateData_with_XPathNavigator()
...{
System.Xml.XPath.XPathNavigator nav = xmlDoc.CreateNavigator();
// Move to the first non-comment element.
nav.MoveToChild(System.Xml.XPath.XPathNodeType.Element);
System.Xml.XPath.XPathNodeIterator nodeIterator = nav.SelectChildren(System.Xml.XPath.XPathNodeType.Element);
while (nodeIterator.MoveNext())
...{
}
}
/**//// <summary>
/// 从Xml中读取数据到类
/// </summary>
public void Read_ClassData_From_Xml()
...{
System.Xml.XmlSerializer reader = new System.Xml.XmlSerializer(typeof(TheClass));
using (System.IO.StreamReader file = new System.IO.StreamReader(@"ClassData.xml"))
...{
TheClass fileData;
fileData = (TheClass)reader.Deserialize(file);
}
}
/**//// <summary>
/// 用XmlTextReader读取数据
/// </summary>
public void Read_Data_With_XmlTextReader()
...{
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("Snippet.xml");
string contents = reader.ReadInnerXml();
}
/**//// <summary>
/// 从字符串读取Xml
/// </summary>
public void Read_Xml_Form_String()
...{
// Create the reader.
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new System.IO.StringReader("<book>"));
while (reader.Read())
...{
}
}
/**//// <summary>
/// 从Url读取Xml
/// </summary>
public void ReadXmlFromUrl()
...{
string myUrl = "http://www.contoso.com/books.xml";
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(myUrl);
while (reader.Read())
...{
}
}
/**//// <summary>
/// 用XPathNavigator读取节点数据,相当与sql查询,当然可以进行更新,查询,删除,添加了
/// </summary>
public void Selet_Nodes_With_XPathNavigator()
...{
System.Xml.XPath.XPathExpression myXPathExpr;
System.Xml.XPath.XPathDocument myXPathDocument = new System.Xml.XPath.XPathDocument(@"data.xml");
// Create an XPathNavigator.
System.Xml.XPath.XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
// Get the Book elements.
string selectExpr = "parent/child";
// Ensure we are at the root node.
myXPathNavigator.MoveToRoot();
myXPathExpr = myXPathNavigator.Compile(selectExpr);
// Create an XPathNodeIterator to walk over the selected nodes.
System.Xml.XPath.XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select(myXPathExpr);
}
/**//// <summary>
/// 用XSl sheet将XMl转化为Html
/// </summary>
public void Transform_Xml_To_Html_Using_Xsl()
...{
using (System.IO.FileStream stream = System.IO.File.Open("output.html", System.IO.FileMode.Create))
...{
// Create XsltCommand compile stylesheet.
System.Xml.Xsl.XslCompiledTransform processor = new System.Xml.Xsl.XslCompiledTransform();
processor.Load("stylesheet.xsl");
// Transform the file.
processor.Transform("data.xml", null, stream);
}
}
/**//// <summary>
/// 用XSlT将XMl转化为Html
/// </summary>
public void Transform_Xml_To_Html_Using_Xslt()
...{
System.Xml.Xsl.XslCompiledTransform xslt = new System.Xml.Xsl.XslCompiledTransform();
xslt.Load("theXsltFile.xslt");
xslt.Transform("theXmlFile.xml", "theOutputFile.html");
}
/**//// <summary>
/// 将类的数据写入Xml
/// </summary>
public void WriteClassDataToXml()
...{
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.ArrayList));
using (System.IO.StreamWriter file = new System.IO.StreamWriter("SerializedData.xml"))
...{
writer.Serialize(file, dataToWrite);
}
}
}
- C#操作Xml全集(1)
- C#操作Xml全集(1)
- C#操作Xml全集示例篇,基本实现添加,删除,更新等操作
- C#操作Xml全集示例篇,基本实现添加,删除,更新等操作
- c#操作xml(1)
- C#对XML操作:建立XML(1)
- C#对XML操作:写入一笔XML记录(1)
- C#操作XML文档(1)
- c#操作xml(2)
- SQL操作全集 (转)
- C#对XML操作:(1/2/3)
- C#对xml的操作笔记(1)
- 什么事XML,C#操作XML
- C#中XML操作简析1
- C#操作XML(增,删,改)
- C#操作XML数据(转)
- .NET C# 操作xml
- C# 操作XML
- 计算机基础与程序设计试题
- 南开C语言习题集
- 应用拷贝构造函数的三种情况
- 使用对象指针访问类的成员
- 访问对象的公有成员函数的不同方式
- C#操作Xml全集(1)
- 通过指针访问类的静态数据成员
- 通过指针访问类的静态成员函数
- C#操作Xml全集示例篇,基本实现添加,删除,更新等操作
- 幻
- asp.net 对xml文件的读写,添加,修改,删除操作
- 动态生成折线图
- PetShop4.0 css
- wingdi 错误