C#中读取xml文件和生成xml文件

1.读取xml文件
using System.Xml;

1-1:

       

        XmlDocument xml = new XmlDocument();
        xml.Load("XMLFile.xml");
        string deviceIP = xml.SelectSingleNode("para/deviceIP").InnerText;
        string devicePort = xml.SelectSingleNode("para/devicePort").InnerText;
        string localIP = xml.SelectSingleNode("para/localIP").InnerText;
        string localPort = xml.SelectSingleNode("para/localPort").InnerText;

1-2:

 

public class BDataStruct
{                     
    public string   mode;
    ublic string   ratioW;
    public string   ratioH;
    public string[] filePath = new string[6];
    public string[] delaytime = new string[6];          
}<pre name="code" class="csharp"> if (File.Exists("ProgramConfig.xml"))
 {
     List<BDataStruct> m_PageDatas;
     m_PageDatas = new List<BDataStruct>();
                              
     XmlDocument xml = new XmlDocument();
     xml.Load("ProgramConfig.xml");

     XmlElement rootElem = xml.DocumentElement;   //获取根节点  
     XmlNodeList childNodes = rootElem.GetElementsByTagName("Para"); //获取Para子节点集合  

     //遍历读取xml的数据
     foreach (XmlNode node in childNodes)
     {                  
        BDataStruct data = new BDataStruct();  
                    
        data.mode = ((XmlElement)node).GetElementsByTagName("mode")[0].InnerText;
        data.ratioW = ((XmlElement)node).GetElementsByTagName("ratioW")[0].InnerText;
        data.ratioH = ((XmlElement)node).GetElementsByTagName("ratioH")[0].InnerText;

        for (int j = 0; j < 6; j++)
        {
            string num = (j + 1).ToString();
            data.filePath[j] = ((XmlElement)node).GetElementsByTagName("path" + num)[0].InnerText;
            data.delaytime[j] = ((XmlElement)node).GetElementsByTagName("delay" + num)[0].InnerText;
        }
        m_PageDatas.Add(data);
      }
  }

 

==================================================================================================
2.生成xml文件

            XmlDocument xmldoc = new XmlDocument();
            XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);
            XmlElement root = xmldoc.CreateElement("", "root", ""); //加入一个根元素
            xmldoc.AppendChild(root);
                 
            XmlElement eb = xmldoc.CreateElement("mode");   
            eb.InnerText = mode;(从外部获取)
            XmlElement ec = xmldoc.CreateElement("ratioW"); 
            ec.InnerText = ratioW;(从外部获取)
            XmlElement ed = xmldoc.CreateElement("ratioH"); 
             ed.InnerText = ratioH;(从外部获取)

            XmlElement ee = xmldoc.CreateElement("program");
            XmlElement ef = xmldoc.CreateElement("time");          
            XmlElement ee1 = xmldoc.CreateElement("program1"); 
            ee1.InnerText = exeString1;(从外部获取)
            XmlElement ef1 = xmldoc.CreateElement("time1");         
            ef1.InnerText = counter1;(从外部获取)                     
            XmlElement ee2 = xmldoc.CreateElement("program2"); 
            ee2.InnerText = exeString2;(从外部获取)
            XmlElement ef2 = xmldoc.CreateElement("time2");         
            ef2.InnerText = counter2;(从外部获取)                           
            XmlElement ee3 = xmldoc.CreateElement("program3"); 
            ee3.InnerText = exeString3;(从外部获取)
            XmlElement ef3 = xmldoc.CreateElement("time3");         
            ef3.InnerText = counter3;(从外部获取)                            
            XmlElement ee4 = xmldoc.CreateElement("program4"); 
            ee4.InnerText = exeString4;(从外部获取)
            XmlElement ef4 = xmldoc.CreateElement("time4");         
            ef4.InnerText = counter4;(从外部获取)
                                       
            root.AppendChild(eb);
            root.AppendChild(ec);
            root.AppendChild(ed);
            ee.AppendChild(ee1);
            ef.AppendChild(ef1);
            ee.AppendChild(ee2);
            ef.AppendChild(ef2);
            ee.AppendChild(ee3);
            ef.AppendChild(ef3);
            ee.AppendChild(ee4);
            ef.AppendChild(ef4);
            root.AppendChild(ee);
            root.AppendChild(ef);      
           
            if (MessageBox.Show("确定保存当前配置信息?", "信息提示",
               MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                xmldoc.Save(@"ProgramConfig.xml");
            }

生成xml文件如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值