日期:2011-06-01 浏览次数:20489 次
public class CXml
{
private string strXmlFile;
private XmlDocument objXmlDoc = new XmlDocument();
public CXml(string xmlFile)
{
//构造函数
try
{
objXmlDoc.Load(xmlFile);
}
catch
{
}
strXmlFile = xmlFile;
}
public DataView GetData(string xmlPathNode)
{
//查找数据返回一个DataView
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(xmlPathNode).OuterXml);
ds.ReadXml(read);
return ds.Tables[0].DefaultView;
}
public void Replace(string xmlPathNode,string content)
{
//更新节点內容
objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
}
public void Delete(string node)
{
//刪除一个节点
string mainNode = node.Substring(0,node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(node));
}
public void InsertNode(string mainNode,string childNode,string element,string content)
{
//插入一节点和此节点的一子节点
XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(childNode);