专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » c#xml类,c#操作xml的完全代码类 »正文

c#xml类,c#操作xml的完全代码类

来源: 发布时间:星期一, 2008年10月6日 浏览:2次 评论:0

一个有用的c#操作xml类,基本上要用的操作都有了

原文出处不详,感谢原作者的贡献

public class xmlControl
 {
  protected string strxmlFile;
  protected xmlDocument objxmlDoc = new xmlDocument();

 

  public xmlControl(string xmlFile)
  {
   //
   // TODO: 在這裡加入建構函式的程式碼
   //
   try
   {
    objxmlDoc.Load(xmlFile);
   }
   catch (System.Exception ex)
   {
    throw ex;
   }
   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);
   objRootNode.AppendChild(objChildNode);
   xmlElement objElement = objxmlDoc.CreateElement(Element);
   objElement.InnerText = Content;
   objChildNode.AppendChild(objElement);
  }

  public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content)
  {
   //插入一個節點,帶一屬性。
   xmlNode objNode = objxmlDoc.SelectSingleNode(MainNode);
   xmlElement objElement = objxmlDoc.CreateElement(Element);
   objElement.SetAttribute(Attrib,AttribContent);
   objElement.InnerText = Content;
   objNode.AppendChild(objElement);
  }

  public void InsertElement(string MainNode,string Element,string Content)
  {
   //插入一個節點,不帶屬性。
   xmlNode objNode = objxmlDoc.SelectSingleNode(MainNode);
   xmlElement objElement = objxmlDoc.CreateElement(Element);
   objElement.InnerText = Content;
   objNode.AppendChild(objElement);
  }

  public void Save()
  {
   //保存文檔。
   try
   {
    objxmlDoc.Save(strxmlFile);
   }
   catch (System.Exception ex)
   {
    throw ex;
   }
   objxmlDoc = null;
  }
 }

=========================================================

实例应用:

   string strxmlFile = Server.MapPath("Testxml.xml");
   xmlControl xmlTool = new xmlControl(strxmlFile);

//   數據顯視
//   dgList.DataSource = xmlTool.GetData("Book/Authors[ISBN=\"0002\"]");
//   dgList.DataBind();

//   更新元素內容
//   xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
//   xmlTool.Save();

//   添加一個新節點
//   xmlTool.InsertNode("Book","Author","ISBN","0004");
//   xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
//   xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
//   xmlTool.Save();

//   刪除一個指定節點的所有內容和屬性
//   xmlTool.Delete("Book/Author[ISBN=\"0004\"]");
//   xmlTool.Save();

//   刪除一個指定節點的子節點
//   xmlTool.Delete("Book/Authors[ISBN=\"0003\"]");
//   xmlTool.Save();
 

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: