代码修改后的版本,全部提交
This commit is contained in:
111
Mtxfw.Utility/XmlClass.cs
Normal file
111
Mtxfw.Utility/XmlClass.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Data;
|
||||
|
||||
namespace Mtxfw.Utility
|
||||
{
|
||||
public class XmlClass
|
||||
{
|
||||
//构造函数
|
||||
public XmlClass()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public XmlDataDocument Create_doc(string xml_path)
|
||||
{
|
||||
//实例化XML操作类
|
||||
XmlDataDocument doc = new XmlDataDocument();
|
||||
try
|
||||
{
|
||||
//加载XML 文件
|
||||
doc.Load(xml_path);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据XML文件创建DataTable列的方法
|
||||
/// </summary>
|
||||
/// <param name="xml_path"></param>
|
||||
/// <param name="rootNode"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable Table_Create(string xml_path, string rootNode)
|
||||
{
|
||||
|
||||
//实例化XML操作对象类
|
||||
XmlDataDocument doc = Create_doc(xml_path);
|
||||
//创建DataTable 类型的变量DT 用来存储读取的XML数据
|
||||
DataTable dt = new DataTable();
|
||||
//获取根节点
|
||||
XmlNode root = doc.SelectSingleNode(rootNode);
|
||||
//循环创建DT列
|
||||
foreach (XmlNode node in root.ChildNodes)
|
||||
{
|
||||
if (node.Name != "#comment")
|
||||
{
|
||||
dt.Columns.Add(node.Attributes["name"].Value, typeof(string));
|
||||
}
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
/// <summary>
|
||||
/// XML读取类
|
||||
/// </summary>
|
||||
/// <param name="xml_path">XML文件地址</param>
|
||||
/// <param name="rootNode">根节点名称</param>
|
||||
/// <returns></returns>
|
||||
public DataTable Xml_Reader(string xml_path, string rootNode)
|
||||
{
|
||||
//实例化XML操作对象类
|
||||
XmlDataDocument doc = Create_doc(xml_path);
|
||||
//创建DataTable 类型的变量DT 用来存储读取的XML数据
|
||||
DataTable dt = new DataTable();
|
||||
//获取根节点
|
||||
XmlNode root = doc.SelectSingleNode(rootNode);
|
||||
//创建行
|
||||
DataRow row = dt.NewRow();
|
||||
//循环添加DT列
|
||||
foreach (XmlNode node in root.ChildNodes)
|
||||
{
|
||||
if (node.Name != "#comment")
|
||||
{
|
||||
dt.Columns.Add(node.Attributes["name"].Value, typeof(string));
|
||||
row[node.Attributes["name"].Value] = node.Attributes["value"].Value;
|
||||
}
|
||||
}
|
||||
dt.Rows.Add(row);
|
||||
return dt;
|
||||
}
|
||||
/// <summary>
|
||||
/// XML数据更新操作
|
||||
/// </summary>
|
||||
/// <param name="xml_path">XML路径</param>
|
||||
/// <param name="rootNode">XML根节点</param>
|
||||
/// <param name="dt">数据更新Table</param>
|
||||
public void Xml_Update(string xml_path, string rootNode, DataTable dt)
|
||||
{
|
||||
//实例化XML操作对象类
|
||||
XmlDataDocument doc = Create_doc(xml_path);
|
||||
//获取根节点
|
||||
XmlNode root = doc.SelectSingleNode(rootNode);
|
||||
//循环更新数据
|
||||
foreach (XmlNode node in root.ChildNodes)
|
||||
{
|
||||
if (node.Name != "#comment")
|
||||
{
|
||||
node.Attributes["value"].Value = dt.Rows[0][node.Attributes["name"].Value].ToString();
|
||||
}
|
||||
}
|
||||
//保存更讯
|
||||
doc.Save(xml_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user