输电线路设计系统中要保存导线数据,首先想到的是数据来保存,但又想了一下,感觉比较麻烦,数据库移植起来
是很麻烦的事,老是出错,反正是做的二次开发包,简单易用,功能强大是首要的,你不可能要你的二次开发用户去配置半天的数据库吧!
如实想到用Xml来存储一些量不大的数据,如气象条件,导线,绝缘子等等,读取方便,并不需要考虑安全性的问题,Xml同样可以进行复杂查询操作,功能强劲。
昨天到微软msdn上查了查,找到了相关的代码示例,现在贴出来,大家可以下载试用,安装到vs2005智能代码段中,非常方便。
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);
}
}
}
{
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);
}
}
}