读写xml文件操作的类

using System;
using System.Xml;
using System.Data;
using System.IO;
namespace _XINYANGT_XML_Manager
{
 /// <summary>
 /// XmlFile 的摘要说明。
 /// </summary>
 public class XmlFile
 {
  private string _filename = null;
  private XmlDocument XmlDoc;
  
  public XmlFile(string filename)
  {

   XmlDoc = new XmlDocument();
   this._filename = filename;
  }


 //增加子节点
  public void AddChildNode(string xPath,string ItemName,string[]Content,string NodeValue)
  {
   try
   {
    XmlDoc.Load(this._filename);
    XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);   
    if(node != null)
    {
     XmlElement elem = XmlDoc.CreateElement(ItemName);
    foreach(string ss in Content)
     {
      if(ss !=null)
      {
       if(ss != "") elem.SetAttribute(ss.Split('=')[0],ss.Split('=')[1]);
      }
     }
     if(NodeValue != null)
     {
      if(NodeValue != "") elem.InnerText=NodeValue;
     }
  
     node.AppendChild(elem);
    }
    XmlTextWriter writer = new XmlTextWriter(this._filename,null);
    writer.Formatting = Formatting.Indented;
    XmlDoc.Save(writer);
    writer.Close();
    writer = null;
   }
   catch(Exception e)
   {
              // 异常写如日志
   }
  }

  //更新某节点
  public void UpdataNode(string xPath,XmlNode NewNode)
  {
   XmlDoc.Load(this._filename);
   XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);   
   if(node != null)
   {
    node.ParentNode.ReplaceChild(NewNode,node);
   }
   XmlTextWriter writer = new XmlTextWriter(this._filename,null);
   writer.Formatting = Formatting.Indented;
   XmlDoc.Save(writer);
   writer.Close();
   writer = null;
  }
     //删除某节点以及其子节点
  public void RemoveNode(string xPath)
  {
   XmlDoc.Load(this._filename);
   XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);   
   if(node != null)
   {
    node.ParentNode.RemoveChild(node);
   }
   XmlTextWriter writer = new XmlTextWriter(this._filename,null);
   writer.Formatting = Formatting.Indented;
   XmlDoc.Save(writer);
   writer.Close();
   writer = null;

  }
  //获取指定节点的属性值
  public string getNodeAttribute(string xPath,string name)
  {
   string result = null;
   try
   {
    XmlDoc.Load(this._filename);
    XmlNode n = XmlDoc.DocumentElement.SelectSingleNode(xPath);
    if(n != null)
    {  
     if(n.LocalName != "#comment" && n.Attributes[name] != null)
     {
      if(n.Attributes["active"] != null )//&& n.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
      {
       result = n.Attributes[name].Value;
      }
      else
      {
       // 异常写如日志
      }
     }
    }
   }
   catch(Exception e)
   {
    // 异常写如日志
   }
   return result;
  }

  //写指定节点的属性值
    public void setNodeAttribute(string xPath,string name,string values)
    {
     try
     {
      XmlDoc.Load(this._filename);
      XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
      if(node != null)
      {  
       if(node.LocalName != "#comment" && node.Attributes[name] != null)
       {
        if(node.Attributes["active"] != null)// && node.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
        {
         node.Attributes[name].Value = values;
        }
        else
        {
         // 异常写如日志
        }
       }
       //添加新属性
       if((!name.Equals("")) && node.Attributes[name] == null)
       {
       /*  
        XmlAttribute NewAttrib;

        NewAttrib =Name = name.ToString().Trim();
           NewAttrib.Value = values;
        node.Attributes.Append(NewAttrib);
       */
       }
      }
      XmlTextWriter writer = new XmlTextWriter(this._filename,null);
      writer.Formatting = Formatting.Indented;
      XmlDoc.Save(writer);
      writer.Close();
      writer = null;     
     }
     catch(Exception e)
     {
        // 异常写如日志
     }
    } 
  public string ChildEx(string xPath)//判断是否有子节点
  {
   try
   {
    XmlDoc.Load(this._filename);
    XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
    if(node.ChildNodes.Count > 0)
    {
     
     return node.ChildNodes.Count.ToString();
    }  
    else
    {
     return node.ChildNodes.Count.ToString();
    }

   }
   catch
   {
    return "";
   }
  }
    
  //获取指定节点的值
  public string getNodeValue(string xPath)
  {
   string result = null;
   try
   {
    XmlDoc.Load(this._filename);
    XmlNode n = XmlDoc.DocumentElement.SelectSingleNode(xPath);
    if(n != null)
    {
     result = n.InnerText;
    }
   }
   catch(Exception e)
   {
    // 异常写如日志
   }
   return result;
  }

  //写指定节点的值
   public void setNodeValue(string xPath,string values)
   {
    try
    {
     XmlDoc.Load(this._filename);
     XmlNode node = XmlDoc.DocumentElement.SelectSingleNode(xPath);
     if(node != null)
     {
     
      node.InnerText = values;
      XmlTextWriter writer = new XmlTextWriter(this._filename,null);
      writer.Formatting = Formatting.Indented;
      XmlDoc.Save(writer);
      writer.Close();
      writer = null;
     }   
               
    }
    catch(Exception e)
    {
    // 异常写如日志
    }
   
   }
  
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值