C# 写xml文件

本文介绍了一种使用C#和XML来保存弱分类器的方法。该方法通过创建XML文档并定义节点来存储弱分类器的相关参数,如特征索引、阈值等。此过程包括检查文件是否存在、创建必要的XML结构、填充数据以及最终保存到文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 private void saveWeaker(string name,List<PotsuWeaker>strongClassifier,List<double>_alpha)
       {
           try
           {
               XmlDocument xmlDoc = new XmlDocument();
               XmlNode subjects=null;

               if (!File.Exists(@"D:\\weaker.xml"))
               {
                   //Create the xml declaration first 
                   xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));

                   //Create the root node and append into doc 
                   subjects = xmlDoc.CreateElement("subjects");
                   xmlDoc.AppendChild(subjects);
               }
               else
               {
                   xmlDoc.Load("D:\\weaker.xml");
                   subjects = xmlDoc.SelectSingleNode("subjects");
               }
               XmlElement subject = xmlDoc.CreateElement("subject");             
               subject.SetAttribute("name",name);          
               subjects.AppendChild(subject);
               for (int i = 0; i < strongClassifier.Count; i++)
               {
                   // weaker
                   XmlElement weaker = xmlDoc.CreateElement("weaker");
                   subject.AppendChild(weaker);
                   XmlElement findex = xmlDoc.CreateElement("feature_index");
                   findex.InnerText = string.Format("{0}", strongClassifier[i].findex);
                   weaker.AppendChild(findex);
                   XmlElement thr = xmlDoc.CreateElement("threshold");
                   thr.InnerText = string.Format("{0}", strongClassifier[i].thr);
                   weaker.AppendChild(thr);
                   XmlElement p = xmlDoc.CreateElement("parity");
                   p.InnerText = string.Format("{0}", strongClassifier[i].p);
                   weaker.AppendChild(p);
                   XmlElement alpha = xmlDoc.CreateElement("alpha");
                   alpha.InnerText = string.Format("{0}", _alpha[i]);
                   weaker.AppendChild(alpha);
               }

               xmlDoc.Save("D:\\weaker.xml");
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           };

      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值