日期:2014-05-18  浏览次数:20765 次

求C#WinForm添加xml,最好详细的注释
在C#Winform窗体程序 有一个Botton添加按钮 当点击添加按钮实现对XML进行添加
以下是添加后的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<Note>
  <Day year="2009" month="12" day="8">
  <To>黄建国</To>
  <From>李忠</From>
  <Heading>提醒</Heading>
  <Message>不要忘了这个周末和我的约会</Message>
  </Day>
</Note>
帮忙下,急用。谢谢!

------解决方案--------------------
C# code
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
        xmlDoc.AppendChild(xmlDoc.CreateElement("Note"));
        XmlElement day = xmlDoc.CreateElement("Day");
        day.SetAttribute("year", "2009");
        day.SetAttribute("month", "12");
        day.SetAttribute("day", "8");
        xmlDoc.DocumentElement.AppendChild(day);
        day.AppendChild(xmlDoc.CreateElement("To"));
        day["To"].InnerText = "黄建国";
        day.AppendChild(xmlDoc.CreateElement("From"));
        day["From"].InnerText = "李忠";
        day.AppendChild(xmlDoc.CreateElement("Heading"));
        day["Heading"].InnerText = "提醒";
        day.AppendChild(xmlDoc.CreateElement("Message"));
        day["Message"].InnerText = "不要忘了这个周末和我的约会";
        xmlDoc.Save(@"c:\test.xml"));