public class XmlHelper
{
public XmlHelper()
{
}
public enum XmlType
{
File,
String
};
/// <summary>
/// 创建XML文档
/// </summary>
/// <param name="name">根节点名称</param>
/// <param name="type">根节点的一个属性值</param>
/// <returns></returns>
/// .net中调用方法:写入文件中,则:
/// document = XmlOperate.CreateXmlDocument("sex","sexy");
/// document.Save("c:/bookstore.xml");
public static XmlDocument CreateXmlDocument(string name,string type)
{
XmlDocument doc = null;
XmlElement rootEle = null;
try
{
doc = new XmlDocument();
doc.LoadXml("<" + name + "/>");
rootEle = doc.DocumentElement;
rootEle.SetAttribute("type",type);
}
catch (Exception er)
{
throw er;
}
return doc;
}
/// <summary>
/// 读取数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>string</returns>
/**************************************************
* 使用示列:
* XmlHelper.Read(path,"/Node","")
* XmlHelper.Read(path,"/Node/Element[@Attribute='Name']","Attribute")
************************************************/
public static string Read(string path,string node,string attribute)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
}
catch { }
return value;
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
/// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Insert(path,"Element","","Value")
* XmlHelper.Insert(path,"Attribute","Value")
************************************************/
public static void Insert(string path,string element,string attribute,string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute,value);
}
}
else
{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute,value);
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Insert(path,"Value")
************************************************/
public static void Update(string path,string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute,value);
doc.Save(path);
}
catch { }
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Delete(path,"")
* XmlHelper.Delete(path,"Attribute")
************************************************/
public static void Delete(string path,string attribute)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xn.ParentNode.RemoveChild(xn);
else
xe.RemoveAttribute(attribute);
doc.Save(path);
}
catch { }
}
#region 读取XML资源到DataSet中
/// <summary>
/// 读取XML资源到DataSet中
/// </summary>
/// <param name="source">XML资源,文件为路径,否则为XML字符串</param>
/// <param name="xmlType">XML资源类型</param>
/// <returns>DataSet</returns>
public static DataSet GetDataSet(string source,XmlType xmlType)
{
DataSet ds = new DataSet();
if (xmlType == XmlType.File)
{
ds.ReadXml(source);
}
else
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(source);
XmlNodeReader xnr = new XmlNodeReader(xd);
ds.ReadXml(xnr);
}
return ds;
}
#endregion
#region 操作xml文件中指定节点的数据
/// <summary>
/// 获得xml文件中指定节点的节点数据
/// </summary>
/// <param name="TableName"></param>
/// <returns></returns>
public static string GetNodeInfoByNodeName(string path,string nodeName)
{
string XmlString = "";
XmlDocument xml = new XmlDocument();
xml.Load(path);
System.Xml.XmlElement root = xml.DocumentElement;
System.Xml.XmlNode node = root.SelectSingleNode("//" + nodeName);
if (node != null)
{
XmlString = node.InnerText;
}
return XmlString;
}
#endregion
#region 获取一个字符串xml文档中的ds
/// <summary>
/// 获取一个字符串xml文档中的ds
/// </summary>
/// <param name="xml_string">含有xml信息的字符串</param>
public static void get_XmlValue_ds(string xml_string,ref DataSet ds)
{
System.Xml.XmlDocument xd = new XmlDocument();
xd.LoadXml(xml_string);
XmlNodeReader xnr = new XmlNodeReader(xd);
ds.ReadXml(xnr);
xnr.Close();
int a = ds.Tables.Count;
}
#endregion
#region 读取XML资源到DataTable中
/// <summary>
/// 读取XML资源到DataTable中
/// </summary>
/// <param name="source">XML资源,文件为路径,否则为XML字符串</param>
/// <param name="xmlType">XML资源类型:文件,字符串</param>
/// <param name="tableName">表名称</param>
/// <returns>DataTable</returns>
public static DataTable GetTable(string source,XmlType xmlType,string tableName)
{
DataSet ds = new DataSet();
if (xmlType == XmlType.File)
{
ds.ReadXml(source);
}
else
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(source);
XmlNodeReader xnr = new XmlNodeReader(xd);
ds.ReadXml(xnr);
}
return ds.Tables[tableName];
}
#endregion
#region 读取XML资源中指定的DataTable的指定行指定列的值
/// <summary>
/// 读取XML资源中指定的DataTable的指定行指定列的值
/// </summary>
/// <param name="source">XML资源</param>
/// <param name="xmlType">XML资源类型:文件,字符串</param>
/// <param name="tableName">表名</param>
/// <param name="rowIndex">行号</param>
/// <param name="colName">列名</param>
/// <returns>值,不存在时返回Null</returns>
public static object GetTableCell(string source,string tableName,int rowIndex,string colName)
{
DataSet ds = new DataSet();
if (xmlType == XmlType.File)
{
ds.ReadXml(source);
}
else
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(source);
XmlNodeReader xnr = new XmlNodeReader(xd);
ds.ReadXml(xnr);
}
return ds.Tables[tableName].Rows[rowIndex][colName];
}
#endregion
#region 读取XML资源中指定的DataTable的指定行指定列的值
/// <summary>
/// 读取XML资源中指定的DataTable的指定行指定列的值
/// </summary>
/// <param name="source">XML资源</param>
/// <param name="xmlType">XML资源类型:文件,字符串</param>
/// <param name="tableName">表名</param>
/// <param name="rowIndex">行号</param>
/// <param name="colIndex">列号</param>
/// <returns>值,不存在时返回Null</returns>
public static object GetTableCell(string source,int colIndex)
{
DataSet ds = new DataSet();
if (xmlType == XmlType.File)
{
ds.ReadXml(source);
}
else
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(source);
XmlNodeReader xnr = new XmlNodeReader(xd);
ds.ReadXml(xnr);
}
return ds.Tables[tableName].Rows[rowIndex][colIndex];
}
#endregion
#region 将DataTable写入XML文件中
/// <summary>
/// 将DataTable写入XML文件中
/// </summary>
/// <param name="dt">含有数据的DataTable</param>
/// <param name="filePath">文件路径</param>
public static void SaveTableToFile(DataTable dt,string filePath)
{
DataSet ds = new DataSet("Config");
ds.Tables.Add(dt.Copy());
ds.WriteXml(filePath);
}
#endregion
#region 将DataTable以指定的根结点名称写入文件 /// <summary> /// 将DataTable以指定的根结点名称写入文件 /// </summary> /// <param name="dt">含有数据的DataTable</param> /// <param name="rootName">根结点名称</param> /// <param name="filePath">文件路径</param> public static void SaveTableToFile(DataTable dt,string rootName,string filePath) { DataSet ds = new DataSet(rootName); ds.Tables.Add(dt.Copy()); ds.WriteXml(filePath); } #endregion
原文链接:https://www.f2er.com/xml/298498.html