上次把数据库访问类共享出来了,几个同事网上感慨了一番,真的没有想到我还保留5年前的代码,我说,代码本来就是应该被阅读和研究的,代码同样具有思想和情节,这就是在一穷二白的情况下的历史和轨迹。配置管理同样也是一位尽职的史官。
总结一下,上次给出的代码虽然可以直接运行和使用,的确存在大量可以改进的地方,现在列出部分意见:
1、不支持多种数据库连接(例如Oracle、DB2、Access,mySql等);
2、代码封装不彻底,面向对象,起码应该采取工厂方法。
3、不支持多线程,没有连接池的概念,同一时间只能建立一个连接;
4、对于事务处理考虑不够;
5、没有考虑到会话;
6、代码本身性能、语法方面还可以存在大量改进的地方。
......
一下子给出好几十条,看来还是有人研究了。这里就不在展开了,有机会专门开个专题讨论一下数据访问层的设计问题。
今天给出ThroughoutCMS技术共享系列第二章:XML访问类,恐怕这也是我们经常用到的一个关键基础类库,无论采用.net还是采用java、VC、python,只要是上规模的项目,都缺少不了它,关于为什么要采用XML的问题,这里就不在详细讨论了,网上资料有的是。
下面的代码同样是一个产品中的真实文件,只不过是第一版的,直接采用dom的思想,缺少SAX的操作,功能小而全,涉及到XML文件读取和节点操作,同样那句话,如果要用,可以直接拷贝,如果教学,非常实用。
// 本文件包含了XML数据访问的通用函数.
using System ;
using System.Globalization ;
using System.Xml ;
namespace CMS.Lib
{
public class XML
{
private XML(){}
/// <summary>
/// 加载XML文档,返回XmlDocument对象
/// </summary>
/// <param name="sXMLFile">xml文件</param>
/// <returns>加载的XmlDocument对象</returns>
public static XmlDocument LoadXML(string sXMLFile)
{
XmlDocument objXmlDoc=new XmlDocument();
try
{
objXmlDoc.Load(sXMLFile);
}
catch (System.Exception ex)
{
throw ex;
}
return objXmlDoc;
}
/// <summary>
/// 加载XML文档,返回XmlDocument对象
/// </summary>
/// <returns>加载的XmlDocument对象</returns>
public static XmlDocument LoadXML()
{
XmlDocument objXmlDoc = new XmlDocument();
try
{
string str_XML = "";
string str_Sql = "Select Config_Value From Content_Config where Config_ID = 1";
str_XML = DB.ExecuteSqlToSingleValue(str_Sql);
if(!(str_Sql.Equals("")||str_Sql.Equals(null)))
{
objXmlDoc.LoadXml(str_XML);
}
}
catch (System.Exception ex)
{
throw ex;
}
return objXmlDoc;
}
/// <summary>
/// 保存xml文件
/// </summary>
/// <param name="oXmlDoc">XmlDocument文档对象</param>
/// <param name="sXMLFile">文件名称</param>
/// <returns>保存成功,返回true;失败,返回false</returns>
public static bool SaveXML(XmlDocument oXmlDoc,string sXMLFile)
{
bool ret=false;
try
{
oXmlDoc.Save(sXMLFile);
ret=true;
}
catch (System.Exception ex)
{
throw ex;
}
return ret;
}
/// <summary>
/// 获取XML中的指定xpath的节点的字符内容
/// </summary>
/// <param name="oXmlDoc">XmlDocument文档对象</param>
/// <param name="sFields">xpath</param>
/// <returns>节点内文本内容</returns>
public static string GetInnerTextByPath(XmlDocument oXmlDoc,string sFieldPath)
{
XmlNode no=oXmlDoc.SelectSingleNode(sFieldPath);
return no.InnerText;
}
/// <summary>
/// 编号:XML004
/// 增加一个节点
/// </summary>
/// <param name="node">父节点</param>
/// <param name="newElementName">节点名称</param>
/// <returns>返回父节点对象</returns>
public static XmlNode AppendElement( XmlNode node, string newElementName)
{
XmlNode oNode ;
if ( node is XmlDocument )
oNode = node.AppendChild( ((XmlDocument)node).CreateElement( newElementName ) ) ;
else
oNode = node.AppendChild( node.OwnerDocument.CreateElement( newElementName ) ) ;
return oNode ;
}
/// <summary>
/// 增加一个元素内容
/// </summary>
/// <param name="node">父节点</param>
/// <param name="newElementName">节点名称</param>
/// <param name="innerValue">内部文本</param>
/// <returns>返回父节点对象</returns>
public static XmlNode AppendElement( XmlNode node, string newElementName, string innerValue )
{
XmlNode oNode ;
if ( node is XmlDocument )
oNode = node.AppendChild( ((XmlDocument)node).CreateElement( newElementName ) ) ;
else
oNode = node.AppendChild( node.OwnerDocument.CreateElement( newElementName ) ) ;
if ( innerValue != null )
oNode.AppendChild( node.OwnerDocument.CreateTextNode( innerValue ) ) ;
return oNode ;
}
/// <summary>
/// 增加一个节点的属性
/// </summary>
/// <param name="xmlDocument">XmlDocument文档对象</param>
/// <param name="name">属性名称</param>
/// <param name="value">属性值</param>
/// <returns>属性集合</returns>
public static XmlAttribute CreateAttribute( XmlDocument xmlDocument, string name, string value )
{
XmlAttribute oAtt = xmlDocument.CreateAttribute( name ) ;
oAtt.Value = value ;
return oAtt ;
}
/// <summary>
/// 设置一个属性
/// </summary>
/// <param name="node">节点</param>
/// <param name="attributeName">属性名称</param>
/// <param name="attributeValue">属性值</param>
public static void SetAttribute( XmlNode node, string attributeName, string attributeValue )
{
if ( node.Attributes[ attributeName ] != null )
node.Attributes[ attributeName ].Value = attributeValue ;
else
node.Attributes.Append( CreateAttribute( node.OwnerDocument, attributeName, attributeValue ) ) ;
}
/// <summary>
/// 返回一个节点下的子节点集合
/// </summary>
/// <param name="oXmlDoc">XmlDocument文档对象</param>
/// <param name="sFieldPath">节点路径</param>
/// <returns>节点集合</returns>
public static XmlNodeList GetNodeList(XmlDocument oXmlDoc,string sFieldPath)
{
XmlNode inXmlNode=oXmlDoc.SelectSingleNode(sFieldPath);
if (inXmlNode.HasChildNodes)
{
return inXmlNode.ChildNodes;
}
else
{
return null;
}
}
/// <summary>
/// 在指定的XmlDocument节点下添加新的节点
/// </summary>
/// <param name="oXmlDoc">XmlDocument文档对象</param>
/// <param name="string">指定的XmlDocument节点path</param>
/// <param name="newElementName">新的节点</param>
/// <returns></returns>
public static XmlDocument InsertNode(XmlDocument oXmlDoc,string ParentNodePath,XmlNode NewNode)
{
XmlNode oNode=oXmlDoc.SelectSingleNode(ParentNodePath);
oNode.AppendChild(NewNode);
return oXmlDoc;
}
/// <summary>
/// 在指定的XmlDocument节点下添加新的节点集合
/// </summary>
/// <param name="oXmlDoc">XmlDocument文档对象</param>
/// <param name="string">指定的XmlDocument节点path</param>
/// <param name="NewNodes">子节点集合</param>
/// <returns></returns>
public static XmlDocument InsertNode(XmlDocument oXmlDoc,string ParentNodePath,XmlNode[] NewNodes)
{
XmlNode oNode=oXmlDoc.SelectSingleNode(ParentNodePath);
for(int i=0;i<NewNodes.Length;i++)
{
oNode.AppendChild(NewNodes[i]);
}
return oXmlDoc;
}
/// <summary>
/// 在指定的XmlDocument节点下的指定属性值
/// </summary>
/// <param name="oXmlDoc">XmlDocument文档对象</param>
/// <param name="string">指定的XmlDocument节点path</param>
/// <param name="attributeName">属性名称</param>
/// <returns>属性值</returns>
public static string GetAttribute(XmlDocument oXmlDoc,string sFieldPath,string attributeName)
{
XmlNode oNode=oXmlDoc.SelectSingleNode(sFieldPath);
return oNode.Attributes[ attributeName ].Value;
}
public static XmlDocument RemoveNode(XmlDocument oXmlDoc,string sFieldPath,string NodeName)
{
XmlNode oNode=oXmlDoc.SelectSingleNode(sFieldPath);
oNode.RemoveChild(oXmlDoc.SelectSingleNode(sFieldPath+"/"+NodeName));
return oXmlDoc;
}
public static void RemoveAttribute(XmlDocument oXmlDoc,string sFieldPath, string attributeName )
{
XmlNode oNode=oXmlDoc.SelectSingleNode(sFieldPath);
if ( oNode.Attributes[ attributeName ] != null )
{
oNode.Attributes.Remove(oNode.Attributes[attributeName]);
}
//oNode.Attributes[ attributeName ].Value = attributeValue ;}
}
}
}