C# XML读写代码

主要为替代INI的读写,此示例为保存datagridview的columns的部分设置
XML文件格式为:
<?xml version="1.0" encoding="utf-8"?>
<Columns>
  <Columns0 Index="0" Visiable="False" HeadText="ID" Width="100" />
  <Columns1 Index="1" Visiable="True" HeadText="年份" Width="100" />
  <Columns2 Index="2" Visiable="True" HeadText="类型" Width="100" />
  <Columns3 Index="3" Visiable="True" HeadText="编号" Width="100" />
</Columns>


读写示例代码如下:

/// <summary>
        /// 读XML
        /// </summary>
        public void RXml()
        {
            if (File.Exists(Sys_Xml))
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(Sys_Xml);
                XmlNode xn= xdoc.SelectSingleNode("Columns");
                XmlNodeList xnl = xn.ChildNodes;

                foreach (XmlNode xnd in xnl)
                {
                    int idx;
                    int.TryParse(xnd.Attributes["Index"].Value,out idx);
                    if (idx <= dataGridView1.Columns.Count)
                    {
                        bool vis;
                        bool.TryParse(xnd.Attributes["Visiable"].Value,out vis);
                        dataGridView1.Columns[idx].Visible = vis;
                        dataGridView1.Columns[idx].HeaderText = xnd.Attributes["HeadText"].Value;
                        int wid;                        
                        int.TryParse(xnd.Attributes["Width"].Value,out wid);
                        dataGridView1.Columns[idx].Width=wid;
                    }
                }
            }
        }

        /// <summary>
        /// 写XML
        /// </summary>
        public void WXml()
        {
            XmlDocument xdoc = new XmlDocument();
            //创建类型声明节点  
            XmlNode node = xdoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xdoc.AppendChild(node);
                
            //创建根节点    
            XmlNode root = xdoc.CreateElement("Columns");
            xdoc.AppendChild(root);

            foreach (DataGridViewColumn dc in dataGridView1.Columns)
            {
                XmlElement xnode = xdoc.CreateElement("Columns" + dc.Index.ToString());
                xnode.SetAttribute("Index", dc.Index.ToString());
                xnode.SetAttribute("Visiable", dc.Visible.ToString());
                xnode.SetAttribute("HeadText", dc.HeaderText);
                xnode.SetAttribute("Width", dc.Width.ToString());
                root.AppendChild(xnode);
            }
            xdoc.Save(Sys_Xml);
        }




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、付费专栏及课程。

余额充值