using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace testForm
{
class Operation_APPCFG
{
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
XmlDeclaration xmldecl;
///
/// 构造函数
///
public Operation_APPCFG()
{
xmldoc = new XmlDocument();
}
///
/// 创建XML文件的段落声明
///
/// XML编码方式,输入"gb2312"、"utf-8"
/// 独立特性,输入"yes"、"no"或"null",默认为"null"
public void CreateDeclaration(string strEncoding,string strStandalone)
{
if ((strEncoding == null) || (strEncoding == ""))
{
strEncoding = null;
}
if ((strStandalOne== null) || (strStandalOne== ""))
{
strStandalone = null;
}
else
{
if (!strStandalone.Equals("yes") || !strStandalone.Equals("no"))
{
strStandalone = null;
}
}
xmldecl = xmldoc.CreateXmlDeclaration("1.0", strEncoding, strStandalone);
xmldoc.AppendChild(xmldecl);
}
///
/// 创建root元素
///
/// root元素的名称
public void CreateElement(string localName)
{
xmlelem = xmldoc.CreateElement("", localName, "");
xmldoc.AppendChild(xmlelem);
}
///
/// 创建节点的子节点,模式
///
/// 父节点的名称
/// 节点的名称
/// 需创建的子节点的hashtable
public void CreateNodeModeA(string parentName, string NodeName, Hashtable hash)
{
XmlNode root = xmldoc.SelectSingleNode(parentName);
XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个节点
foreach (DictionaryEntry de in hash)
{
xe1.SetAttribute(de.Key.ToString(), de.Value.ToString());//设置该节点genre属性 //xe1.SetAttribute("ISBN", "2-3631-4");
}
root.AppendChild(xe1);
}
///
/// 创建节点的子节点(泛型方法),模式
///
/// 父节点的名称
/// 节点的名称
/// 需创建的子节点的hashtable
public void CreateNodeModeB(string parentName,string NodeName, Dictionary<string, string> hash )
{
XmlNode root = xmldoc.SelectSingleNode(parentName);
XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个节点
foreach (KeyValuePair<string, string> de in hash)
{
xe1.SetAttribute(de.Key.ToString(), de.Value.ToString());//设置该节点genre属性 //xe1.SetAttribute("ISBN", "2-3631-4");
}
root.AppendChild(xe1);
}
///
/// 创建节点的子节点(泛型方法),模式
///
/// 节点的名称
/// 需创建的子节点的hashtable
public void CreateNodeModeC(string parentName,Hashtable hash)
{
XmlNode root = xmldoc.SelectSingleNode(parentName);
XmlElement xesub1;
foreach (DictionaryEntry de in hash)
{
xesub1 = xmldoc.CreateElement(de.Key.ToString());
xesub1.InnerText = de.Value.ToString();//设置文本节点
root.AppendChild(xesub1);
}
}
///
/// 保存文件
///
///
public void SaveFile(string pathFile)
{
xmldoc.Save(pathFile);
}
}
}
下面是网上一位网友写的示例,参考意义相同,原文地址http://www.cnblogs.com/txw1958/archive/2013/01/16/csharp-xml.html
public void CreateFile()
{
//加入XML的声明段落,
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", null, null);//encoding mode:"gb2312"、"utf-8",也可以为null
xmldoc.AppendChild(xmldecl);
//加入一个根元素
xmlelem = xmldoc.CreateElement("", "configuration", "");
xmldoc.AppendChild(xmlelem);
//加入另外一个元素
for (int i = 1; i <3; i++)
{
XmlNode root = xmldoc.SelectSingleNode("Employees");//查找
XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个节点
xe1.SetAttribute("genre", "DouCube");//设置该节点genre属性
xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性
XmlElement xesub1 = xmldoc.CreateElement("title");
xesub1.InnerText = "CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到节点中
XmlElement xesub2 = xmldoc.CreateElement("author");
xesub2.InnerText = "候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmldoc.CreateElement("price");
xesub3.InnerText = "58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到节点中
}
//保存创建好的XML文档
xmldoc.Save("DB.cfg");
}
https://msdn.microsoft.com/zh-cn/library/system.configuration.configuration(v=vs.80).aspx
https://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument.createelement(v=vs.100).aspx