C#.NET Xml操作

 

  
    
using System ;
using System.Data ;
using System.Xml ;
using System.IO;

namespace Pub.Class
{
/// <summary>
/// Xmls操作类
/// </summary>
public class Xml2
{
#region 私有成员
private string strXmlFile;
private XmlDocument objXmlDoc = new XmlDocument();
private int _State = 0 ;
#endregion

#region 属性
/// <summary>
/// 返回操作状态
/// </summary>
public int State { get { return _State; } }
#endregion

#region 构造器
/// <summary>
/// 构造器
/// </summary>
/// <param name="XmlFile"></param>
public Xml2( string XmlFile){
strXmlFile
= XmlFile;
try { objXmlDoc.Load(XmlFile);}
catch { _State = 1 ; } // 无法加载XML文件
}
#endregion

#region GetData
/// <summary>
/// 返回XML文件所有数据
/// </summary>
/// <returns> DataSet </returns>
public DataSet GetData()
{
DataSet ds
= new DataSet();
ds.ReadXml(@strXmlFile);
return ds;
}
/// <summary>
/// 返回指定结点的所有数据
/// </summary>
/// <param name="Node"> 结点 </param>
/// <returns> DataSet </returns>
public DataSet GetData( string Node)
{
string mainNode = Node.TrimEnd( ' / ' );
DataSet ds
= new DataSet();
StringReader read
= new StringReader(objXmlDoc.SelectSingleNode(mainNode).OuterXml);
ds.ReadXml(read);
return ds;
}
#endregion

#region Node/DelNode/GetNodeText/SetNodeText/AddNode
/// <summary>
/// 取结点的内容
/// </summary>
/// <param name="Node"> 结点 </param>
/// <returns> 内容 </returns>
public string GetNodeText( string Node) {
string mainNode = Node.TrimEnd( ' / ' ),_value = "" ;
XmlNode objNode
= objXmlDoc.SelectSingleNode(mainNode);
_value
= objNode.InnerText;
return _value;
}

/// <summary>
/// 重新设置结点的内容
/// </summary>
/// <param name="Node"> 结点 </param>
/// <param name="Content"> 内容 </param>
public void SetNodeText( string Node, string Content)
{
string mainNode = Node.TrimEnd( ' / ' );
XmlNode objNode
= objXmlDoc.SelectSingleNode(mainNode);
objNode.InnerText
= Content;
}

/// <summary>
/// 添加有内容的结点。属性名,属性值支持用“|”分开的字符串
/// </summary>
/// <param name="MainNode"> 当前结点 </param>
/// <param name="Node"> 新结点名 </param>
/// <param name="attributeName"> 属性名 </param>
/// <param name="attributeValue"> 属性值 </param>
/// <param name="Content"> 内容 </param>
public void AddNode( string MainNode, string Node, string attributeName, string attributeValue, string Content)
{
string _mainNode = MainNode.TrimEnd( ' / ' );
string [] attributeNameArr = attributeName.Split( ' | ' ), attributeValueArr = attributeValue.Split( ' | ' );
if (attributeValueArr.Length != attributeNameArr.Length) { _State = 3 ; return ; } // 参数不正确
XmlNode objNode = objXmlDoc.SelectSingleNode(_mainNode);
XmlElement objElement
= objXmlDoc.CreateElement(Node);
if (attributeName.Trim() != "" ) {
for ( int i = 0 ; i <= attributeNameArr.Length - 1 ; i ++ ) {
if (objNode.Attributes[attributeNameArr[i]] == null ) {
objElement.SetAttribute(attributeNameArr[i], attributeValueArr[i]);
}
}
}
objElement.InnerText
= Content;
objNode.AppendChild(objElement);
}

/// <summary>
/// 添加无内容的结点。属性名,属性值支持用“|”分开的字符串
/// </summary>
/// <param name="MainNode"> 当前结点 </param>
/// <param name="Node"> 新结点名 </param>
/// <param name="attributeName"> 属性名 </param>
/// <param name="attributeValue"> 属性值 </param>
public void AddNode( string MainNode, string Node, string attributeName, string attributeValue)
{
string _mainNode = MainNode.TrimEnd( ' / ' );
string [] attributeNameArr = attributeName.Split( ' | ' ), attributeValueArr = attributeValue.Split( ' | ' );
if (attributeValueArr.Length != attributeNameArr.Length) { _State = 3 ; return ; } // 参数不正确
XmlNode objNode = objXmlDoc.SelectSingleNode(_mainNode);
XmlElement objElement
= objXmlDoc.CreateElement(Node);
if (attributeName.Trim() != "" ) {
for ( int i = 0 ; i <= attributeNameArr.Length - 1 ; i ++ ) {
if (objNode.Attributes[attributeNameArr[i]] == null ) {
objElement.SetAttribute(attributeNameArr[i], attributeValueArr[i]);
}
}
}
objNode.AppendChild(objElement);
}
/// <summary>
/// 删除结点
/// </summary>
/// <param name="MainNode"></param>
public void DelNode( string MainNode)
{
string _mainNode = MainNode.TrimEnd( ' / ' );
XmlNode objNode
= objXmlDoc.SelectSingleNode(_mainNode);
if (objNode != null ) objNode.ParentNode.RemoveChild(objNode);
}
#endregion

#region Attr
/// <summary>
/// 取指定结点的属性值,属性名支持用“|”分开的字符串
/// </summary>
/// <param name="Node"> 结点 </param>
/// <param name="attributeName"> 属性名 </param>
/// <returns></returns>
public string GetAttr( string Node, string attributeName) {
string mainNode = Node.TrimEnd( ' / ' ),_value = "" ;
string [] attributeNameArr = attributeName.Split( ' | ' );
XmlNode objNode
= objXmlDoc.SelectSingleNode(mainNode);
for ( int i = 0 ; i <= attributeNameArr.Length - 1 ; i ++ ) {
try {
_value
+= objNode.Attributes[attributeNameArr[i]].Value.ToString() + " | " ;
}
catch { _value += " | " ; }
}
return _value.Substring( 0 ,_value.Length - 1 );
}

/// <summary>
/// 为指定结点添加新的属性值,如果存在则不添加。属性名,属性值支持用“|”分开的字符串
/// </summary>
/// <param name="Node"> 结点 </param>
/// <param name="attributeName"> 属性名 </param>
/// <param name="attributeValue"> 属性值 </param>
public void AddAttr( string Node, string attributeName, string attributeValue)
{
string _mainNode = Node.TrimEnd( ' / ' );
string [] attributeNameArr = attributeName.Split( ' | ' ), attributeValueArr = attributeValue.Split( ' | ' );
if (attributeValueArr.Length != attributeNameArr.Length) { _State = 3 ; return ; } // 参数不正确
XmlElement objElement = (XmlElement)objXmlDoc.SelectSingleNode(_mainNode);
try {
for ( int i = 0 ; i <= attributeNameArr.Length - 1 ; i ++ ) {
if (objElement.Attributes[attributeNameArr[i]] == null ){
objElement.SetAttribute(attributeNameArr[i], attributeValueArr[i]);
}
}
}
catch { _State = 4 ; } // 操作错误
}

/// <summary>
/// 设置指定的属性值,此属性必需存在。属性名,属性值支持用“|”分开的字符串
/// </summary>
/// <param name="Node"> 结点 </param>
/// <param name="attributeName"> 属性名 </param>
/// <param name="attributeValue"> 属性值 </param>
public void SetAttr( string Node, string attributeName, string attributeValue)
{
string mainNode = Node.TrimEnd( ' / ' );
string [] attributeNameArr = attributeName.Split( ' | ' ), attributeValueArr = attributeValue.Split( ' | ' );
if (attributeValueArr.Length != attributeNameArr.Length) { _State = 3 ; return ; } // 参数不正确
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
for ( int i = 0 ; i <= attributeNameArr.Length - 1 ; i ++ ) {
try {
objNode.Attributes[attributeNameArr[i]].Value
= attributeValueArr[i];
}
catch { }
}
}
#endregion

#region 保存XML文件
/// <summary>
/// 保存XML文件
/// </summary>
/// <example>
/// <code>
/// string strXmlFile = Server.MapPath("~/web.config");
/// Xml _xml = new Xmls(strXmlFile);
/// _xml.AddNode("configuration//appSettings","add", "key|value", "12|1111111");
/// _xml.AddNode("configuration//appSettings", "add", "key|value", "12|1111111", "cexo255");
/// Response.Write(_xml.getNodeText("configuration//appSettings//add[@key='12']"));
/// _xml.SetAttr("configuration//appSettings//add[@key='']", "value|providerName", "aaaaaaaaaaaa3|System.Data.SqlClient3");
/// _xml.AddAttr("configuration//appSettings//add[@key='']", "value|providerName","aaaaaaaaaaaa|System.Data.SqlClient");
/// Response.Write(_xml.getAttr("configuration//appSettings//add[@key='']", "value|providerName"));
/// _xml.Save();
/// switch (_xml.State) {
/// case 0:
/// Js.Alert(this, "操作成功!");
/// break;
/// case 1:
/// Js.Alert(this, "无法加载XML文件");
/// break;
/// case 2:
/// Js.Alert(this, "保存失败");
/// break;
/// case 3:
/// Js.Alert(this, "参数对应不正确");
/// break;
/// case 4:
/// Js.Alert(this, "操作错误");
/// break;
/// }
/// </code>
/// </example>
public void Save() {
try { if (_State == 0 ) objXmlDoc.Save(strXmlFile); }
catch { _State = 2 ; } // 保存失败
objXmlDoc = null ;
}
/// <summary>
/// 关闭XML对像
/// </summary>
public void Close() {
if ( null != objXmlDoc) { objXmlDoc = null ;}
}
public string ToXmlText(){
return objXmlDoc.OuterXml;
}
#endregion

#region 全局方法Create
/// <summary>
/// 新建一个XML文件
/// </summary>
/// <param name="xmlFile"> XML文件路径 </param>
/// <param name="cssFile"> CSS文件路径 </param>
/// <param name="xlsFile"> XLS文件路径 </param>
/// <param name="encoding"> 编码 </param>
/// <param name="node"> 根结点 </param>
/// <returns> 是否操作成功 </returns>
public static bool Create( string xmlFile, string cssFile, string xlsFile, string encoding, string node) {
if (node.Trim().Equals( "" )) return false ;
if (encoding.Trim().Equals( "" )) encoding = " utf-8 " ;
string _str = " <?xml version=\ " 1.0 \ " encoding=\ "" + encoding + " \ " ?> " ;
if ( ! cssFile.Trim().Equals( "" )) _str += Environment.NewLine + " <?xml-stylesheet type=\ " text / css\ " href=\ "" + cssFile + " \ " ?> " ;
if ( ! xlsFile.Trim().Equals( "" )) _str += Environment.NewLine + " <?xml-stylesheet type=\ " text / xsl\ " href=\ "" + xlsFile + " \ " media=\ " screen\ " ?> " ;
_str
+= Environment.NewLine + node;
return Log.Write(xmlFile, _str, encoding);
}
#endregion
}
}



 

你可能感兴趣的:(.net)