WinForm编程之XML读写

1 using System;
  2   using System.Data;
  3 using System.Configuration;
  4 using System.Web;
  5 using System.Web.Security;
  6 using System.Web.UI;
  7 using System.Web.UI.WebControls;
  8 using System.Web.UI.WebControls.WebParts;
  9 using System.Web.UI.HtmlControls;
10 using System.Collections;
11
12 // 引用命名空间
13 using System.Xml;
14  
16 namespace xmlOp
17 {
18 /// <summary>
19 /// XmlOp类提供对XML数据库的读写
20 /// </summary>
21     public class XmlOp
22     {
23         // 声明一个XmlDocument空对象
24         protected XmlDocument XmlDoc = new XmlDocument();
25
26         /// <summary>
27         /// 构造函数,导入Xml文件
28         /// </summary>
29         /// <param name="xmlFile"> 文件虚拟路径 </param>
30         public XmlOp( string xmlFile)
31         {
32             try
33             {
34                 XmlDoc.Load(GetXmlFilePath(xmlFile));   // 载入Xml文档
35             }
36             catch (System.Exception ex)
37             {
38                 throw ex;
39             }
40         }
41         /// <summary>
42         /// 析构函数
43         /// </summary>
44         ~ XmlOp()
45         {
46             XmlDoc = null // 释放XmlDocument对象
47         }
48
49         /// <summary>
50         /// 保存文件
51         /// </summary>
52         /// <param name="filePath"> 文件虚拟路径 </param>
53         public void Save( string filePath)
54         {
55             try
56             {
57                 XmlDoc.Save(GetXmlFilePath(filePath));
58             }
59             catch (System.Exception ex)
60             {
61                 throw ex;
62             }
63         }
64
65         /// <summary>
66         /// 返回Xml文件实际路径
67         /// </summary>
68         /// <param name="xmlFile"> 文件虚拟路径 </param>
69         /// <returns></returns>
70         public string GetXmlFilePath( string xmlFile)
71         {
72             return HttpContext.Current.Server.MapPath(xmlFile);
73         }
74
75         /// <summary>
76         /// 根据Xml文件的节点路径,返回一个DataSet数据集
77         /// </summary>
78         /// <param name="XmlPathNode"> Xml文件的某个节点 </param>
79         /// <returns></returns>
80         public DataSet GetDs( string XmlPathNode)
81         {
82             DataSet ds = new DataSet();
83             try
84          <
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# XML文件读写操作源码,以及如何调用,注释详解,有任何问题请留言, 以下截取xml文件和部分调用代码段: * ++++++++++++++++++++++++++++++++++++++ <?xml version="1.0" encoding="utf-8" standalone="no"?> <!--TestPlugins的信息--> <!--DataPlugins的信息--> * ++++++++++++++++++++++++++++ xml xl = new xml(); xl.XMLWriteRootNode("info"); //XmlElement Eml1 = xl.XMLReadNode("",0); //XmlElement Eml2 = xl.XMLReadNode("DataPlugins", 1); //XmlElement Eml4 = xl.XMLReadNode("DeviceInfo", 2); // TestPlugins XmlElement testPlugins = xl.XMLCreateNode("TestPlugins", null, null); xl.XMLInsertNode("info", 0, "TestPlugins的信息", testPlugins); // FixturePlugin XmlElement fixturePlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "FixturePlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, fixturePlugin); // DUTPlugin XmlElement DUTPlugin = (XmlElement)fixturePlugin.CloneNode(true);// xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "DUTPlugin" } }); DUTPlugin.SetAttribute("Type", "DUTPlugin"); xl.XMLInsertNode(testPlugins.LocalName, 1, null, DUTPlugin); // Agilent34461APlugin XmlElement Agilent34461APlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "Agilent34461APlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, Agilent34461APlugin); // ================================== // DataPlugins XmlElement dataPlugins = xl.XMLCreateNode("DataPlugins", null, null); xl.XMLInsertNode("info", 0, "DataPlugins的信息", dataPlugins); // CSVLogPlugin XmlElement csvlogPlugin = xl.XMLCreateNode("DataPlugin", null, new Dictionary() { { "Type", "CSVLogPlugin" } }); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, csvlogPlugin); XmlElement uartlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); uartlogPlugin.SetAttribute("Type", "UartLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, uartlogPlugin); XmlElement testlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); testlogPlugin.SetAttribute("Type", "TestLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, testlogPlugin); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值