C# 写xml文件

来源:互联网 发布:js escape编码中文 编辑:程序博客网 时间:2024/05/02 04:44
 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);           };      }

原创粉丝点击