C#.net对XML文件的操作,福cs源代码

王朝c#·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

C#.net对XML文件类封装:这个东西是很早的时候写的,是基于COM模型的,所以大家还可以优化。下面我把这样实现对XML文件操作的类的C#.net代码贴出,如大家要转载,请保留本人的版权。

/*

*Description:完全的操作XML文件

*Auther:mingziweb_天很蓝

*Email:chongchong2008@msn.com

*Dates:22004-09-10

*Copyright:ChongChong2008 YiChang HuBei China

*/

using System;

using System.Xml;

using System.Xml.Xsl;

using System.Xml.XPath;

using System.Web;

namespace Library.ClassLibrary.XML

{

/// <summary>

/// xml 的摘要说明。

/// </summary>

public class XML

{

public enum enumXmlPathType

{

AbsolutePath,

VirtualPath

}

private string xmlFilePath ;

private enumXmlPathType xmlFilePathType ;

private XmlDocument xmlDoc = new XmlDocument() ;

public string XmlFilePath

{

set

{

xmlFilePath = value ;

}

}

public enumXmlPathType XmlFilePathTyp

{

set

{

xmlFilePathType = value ;

}

}

public XML( string tempXmlFilePath )

{

//

// TODO: 在此处添加构造函数逻辑

//

this.xmlFilePathType = enumXmlPathType.VirtualPath ;

this.xmlFilePath = tempXmlFilePath ;

GetXmlDocument() ;

//xmlDoc.Load( xmlFilePath ) ;

}

public XML( string tempXmlFilePath , enumXmlPathType tempXmlFilePathType )

{

//

// TODO: 在此处添加构造函数逻辑

//

this.xmlFilePathType = tempXmlFilePathType ;

this.xmlFilePath = tempXmlFilePath ;

GetXmlDocument() ;

}

/// </summary>

/// <param name="strEntityTypeName">实体类的名称</param>

/// <returns>指定的XML描述文件的路径</returns>

private XmlDocument GetXmlDocument()

{

XmlDocument doc=null;

if( this.xmlFilePathType == enumXmlPathType.AbsolutePath )

{

doc = GetXmlDocumentFromFile( xmlFilePath ) ;

}

else if( this.xmlFilePathType == enumXmlPathType.VirtualPath )

{

doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(xmlFilePath)) ;

}

return doc;

}

private XmlDocument GetXmlDocumentFromFile(string tempXmlFilePath)

{

string xmlFileFullPath = tempXmlFilePath ;

xmlDoc.Load(xmlFileFullPath) ;

return xmlDoc ;

}

#region 读取指定节点的指定属性值

/// <summary>

/// 功能:

/// 读取指定节点的指定属性值

///

/// 参数:

/// 参数一:节点名称

/// 参数二:此节点的属性

/// </summary>

/// <param name="strNode"></param>

/// <param name="strAttribute"></param>

/// <returns></returns>

public string GetXmlNodeValue(string strNode,string strAttribute)

{

string strReturn = "";

try

{

//根据指定路径获取节点

XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode) ;

//获取节点的属性,并循环取出需要的属性值

XmlAttributeCollection xmlAttr = xmlNode.Attributes ;

for(int i=0 ;i<xmlAttr.Count; i++)

 

[1] [2] [3] [4] [5] [6] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航