C# XML文件操作
XML文件内容样例
<?xml version="1.0" encoding="utf-8"?>
<DataBaseInfo>
<Ad>
<ServerName>172.16.78.198</ServerName>
<UserName>sa</UserName>
<UserPwd>1</UserPwd>
<DataBase>dcdemo</DataBase>
</Ad>
</DataBaseInfo>
<DataBaseInfo>
<Ad>
<ServerName>172.16.78.198</ServerName>
<UserName>sa</UserName>
<UserPwd>1</UserPwd>
<DataBase>dcdemo</DataBase>
</Ad>
</DataBaseInfo>
创建XML文件,写入XML文件,读取XML文件
/// <summary>
/// 创建XML文件
/// </summary>
/// <param name="filename"></param>
public static void CreateXmlFile(string filename, string Server, string User, string Pwd, string DataBase)
{
XmlDocument xmldoc = new XmlDocument();
XmlNode node;
node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(node);
//创建根节点
XmlNode root = xmldoc.CreateElement("DataBaseInfo");
xmldoc.AppendChild(root);
XmlNode node1 = xmldoc.CreateNode(XmlNodeType.Element, "Ad", null);
CreateNode(xmldoc, node1, "ServerName", Server);
CreateNode(xmldoc, node1, "UserName", User);
CreateNode(xmldoc, node1, "UserPwd", Pwd);
CreateNode(xmldoc, node1, "DataBaee", DataBase);
root.AppendChild(node1);
try
{
xmldoc.Save(filename);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 写入XML文件
/// </summary>
/// <param name="file"></param>
public static void WriteXmlFile(string file, string Server, string User, string Pwd, string DataBase)
{
XmlDocument xmdoc = new XmlDocument();
try
{
xmdoc.Load(file);
XmlNode root = xmdoc.SelectSingleNode("DataBaseInfo");
if (root != null)
{
XmlNode node = xmdoc.CreateNode(XmlNodeType.Element, "Ad", null);
CreateNode(xmdoc, node, "ServerName", Server);
CreateNode(xmdoc, node, "UserName", User);
CreateNode(xmdoc, node, "UserPwd", Pwd);
CreateNode(xmdoc, node, "DataBaee", DataBase);
root.AppendChild(node);
}
xmdoc.Save(file);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 创建节点
/// </summary>
/// <param name="xmldoc"></param>
/// <param name="parentnode"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public static void CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value)
{
XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, name, null);
node.InnerText = value;
parentnode.AppendChild(node);
}
/// <summary>
/// 获取登录到数据库的用户名
/// </summary>
public static string GetDBUserName(string DataBaseInfo)
{
//string DataBaseInfo = Application.StartupPath;
DataBaseInfo = DataBaseInfo + "\\DataBase.xml";
string dbUser = "";
if (File.Exists(DataBaseInfo))
{
XmlDocument xmdoc = new XmlDocument();
xmdoc.Load(DataBaseInfo);
//搜索指定的节点
System.Xml.XmlNodeList nodes = xmdoc.SelectNodes("//Ad");
if (nodes != null)
{
foreach (System.Xml.XmlNode xn in nodes)
{
dbUser = xn.SelectSingleNode("UserName").InnerText.ToString();
}
}
}
return dbUser;
}
/// <summary>
/// 读取XML文档
/// </summary>
public static DataTable GetSelectedProduceInfo(string produceInfo, string produceNum)
{
//string produceInfo = Application.StartupPath;
produceInfo = produceInfo + "\\ProduceInfo.xml";
DataTable dt = new DataTable();
if (File.Exists(produceInfo))
{
XmlDocument xmdoc = new XmlDocument();
xmdoc.Load(produceInfo);
DataRow dr;
dt.Columns.Add("ProduceNum", typeof(string));//产品编号
dt.Columns.Add("ProduceName", typeof(string));//产品名称
dt.Columns.Add("ProduceSelect", typeof(string));//是否被选择
dt.Columns.Add("ProduceSelectStart", typeof(string));//是否默认启动
dt.Columns.Add("ProducePath", typeof(string));
dt.Columns.Add("ProduceModuleStart", typeof(string));
dr = dt.NewRow();
System.Xml.XmlNodeList nodes = xmdoc.SelectNodes("//Ad");
if (nodes != null)
{
foreach (System.Xml.XmlNode xn in nodes)
{
if (xn.SelectSingleNode("ProduceNum").InnerText.ToString() == produceNum)
{
dr[0] = xn.SelectSingleNode("ProduceNum").InnerText.ToString();
dr[1] = xn.SelectSingleNode("ProduceName").InnerText.ToString();
dr[2] = xn.SelectSingleNode("ProduceSelect").InnerText.ToString();
dr[3] = xn.SelectSingleNode("ProduceSelectStart").InnerText.ToString();
dr[4] = xn.SelectSingleNode("ProducePath").InnerText.ToString();
dr[5] = xn.SelectSingleNode("ProduceModuleStart").InnerText.ToString();
if (dr != null)
{
dt.Rows.Add(dr);
}
break;
}
}
}
}
return dt;
}
/// 创建XML文件
/// </summary>
/// <param name="filename"></param>
public static void CreateXmlFile(string filename, string Server, string User, string Pwd, string DataBase)
{
XmlDocument xmldoc = new XmlDocument();
XmlNode node;
node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(node);
//创建根节点
XmlNode root = xmldoc.CreateElement("DataBaseInfo");
xmldoc.AppendChild(root);
XmlNode node1 = xmldoc.CreateNode(XmlNodeType.Element, "Ad", null);
CreateNode(xmldoc, node1, "ServerName", Server);
CreateNode(xmldoc, node1, "UserName", User);
CreateNode(xmldoc, node1, "UserPwd", Pwd);
CreateNode(xmldoc, node1, "DataBaee", DataBase);
root.AppendChild(node1);
try
{
xmldoc.Save(filename);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 写入XML文件
/// </summary>
/// <param name="file"></param>
public static void WriteXmlFile(string file, string Server, string User, string Pwd, string DataBase)
{
XmlDocument xmdoc = new XmlDocument();
try
{
xmdoc.Load(file);
XmlNode root = xmdoc.SelectSingleNode("DataBaseInfo");
if (root != null)
{
XmlNode node = xmdoc.CreateNode(XmlNodeType.Element, "Ad", null);
CreateNode(xmdoc, node, "ServerName", Server);
CreateNode(xmdoc, node, "UserName", User);
CreateNode(xmdoc, node, "UserPwd", Pwd);
CreateNode(xmdoc, node, "DataBaee", DataBase);
root.AppendChild(node);
}
xmdoc.Save(file);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 创建节点
/// </summary>
/// <param name="xmldoc"></param>
/// <param name="parentnode"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public static void CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value)
{
XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, name, null);
node.InnerText = value;
parentnode.AppendChild(node);
}
/// <summary>
/// 获取登录到数据库的用户名
/// </summary>
public static string GetDBUserName(string DataBaseInfo)
{
//string DataBaseInfo = Application.StartupPath;
DataBaseInfo = DataBaseInfo + "\\DataBase.xml";
string dbUser = "";
if (File.Exists(DataBaseInfo))
{
XmlDocument xmdoc = new XmlDocument();
xmdoc.Load(DataBaseInfo);
//搜索指定的节点
System.Xml.XmlNodeList nodes = xmdoc.SelectNodes("//Ad");
if (nodes != null)
{
foreach (System.Xml.XmlNode xn in nodes)
{
dbUser = xn.SelectSingleNode("UserName").InnerText.ToString();
}
}
}
return dbUser;
}
/// <summary>
/// 读取XML文档
/// </summary>
public static DataTable GetSelectedProduceInfo(string produceInfo, string produceNum)
{
//string produceInfo = Application.StartupPath;
produceInfo = produceInfo + "\\ProduceInfo.xml";
DataTable dt = new DataTable();
if (File.Exists(produceInfo))
{
XmlDocument xmdoc = new XmlDocument();
xmdoc.Load(produceInfo);
DataRow dr;
dt.Columns.Add("ProduceNum", typeof(string));//产品编号
dt.Columns.Add("ProduceName", typeof(string));//产品名称
dt.Columns.Add("ProduceSelect", typeof(string));//是否被选择
dt.Columns.Add("ProduceSelectStart", typeof(string));//是否默认启动
dt.Columns.Add("ProducePath", typeof(string));
dt.Columns.Add("ProduceModuleStart", typeof(string));
dr = dt.NewRow();
System.Xml.XmlNodeList nodes = xmdoc.SelectNodes("//Ad");
if (nodes != null)
{
foreach (System.Xml.XmlNode xn in nodes)
{
if (xn.SelectSingleNode("ProduceNum").InnerText.ToString() == produceNum)
{
dr[0] = xn.SelectSingleNode("ProduceNum").InnerText.ToString();
dr[1] = xn.SelectSingleNode("ProduceName").InnerText.ToString();
dr[2] = xn.SelectSingleNode("ProduceSelect").InnerText.ToString();
dr[3] = xn.SelectSingleNode("ProduceSelectStart").InnerText.ToString();
dr[4] = xn.SelectSingleNode("ProducePath").InnerText.ToString();
dr[5] = xn.SelectSingleNode("ProduceModuleStart").InnerText.ToString();
if (dr != null)
{
dt.Rows.Add(dr);
}
break;
}
}
}
}
return dt;
}