在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>
帮忙下,急用。谢谢!
以下是添加后的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>
帮忙下,急用。谢谢!
<DATA>
<ID>3</ID>
<NAME>user3</NAME>
<Explain>姓名</Explain>
</DATA> XmlDocument xmldoc ;
XmlNode xmlnode ;
XmlElement xmlelem ;
XmlElement xmlelem1 ;
XmlText xmltext ;
XmlTextWriter XMLWriter;//添加节点
//初始化
xmldoc = new XmlDocument ();
xmldoc.Load ("Data.xml");
xmlnode = xmldoc.DocumentElement;
//添加一个名为DATA的子节点
xmlelem = xmldoc.CreateElement ("DATA") ;
//此子节点下的元素
xmlelem1 = xmldoc.CreateElement ("ID") ;
xmltext = xmldoc.CreateTextNode ("3") ;
xmlelem1.AppendChild ( xmltext ) ;
xmlelem.AppendChild ( xmlelem1 ) ;xmlelem1 = xmldoc.CreateElement ("NAME") ;
xmltext = xmldoc.CreateTextNode ("user3") ;
xmlelem1.AppendChild ( xmltext ) ;
xmlelem.AppendChild ( xmlelem1 ) ;xmlelem1 = xmldoc.CreateElement ("Explain") ;
xmltext = xmldoc.CreateTextNode ("姓名:") ;
xmlelem1.AppendChild ( xmltext ) ;
xmlelem.AppendChild ( xmlelem1 ) ;//结束此子节点
xmldoc.DocumentElement.AppendChild(xmlelem);
//保存
xmldoc.Save("Data.xml");
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"));
private static void CreateXml()
{
XmlDocument xmldoc = new XmlDocument();
XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode); XmlElement xmlelem = xmldoc.CreateElement("", "Note", "");
xmldoc.AppendChild(xmlelem); XmlElement xmlelem1 = xmldoc.CreateElement("", "Day", "");//程序版本号
xmlelem1.SetAttribute("year", "2009");
xmlelem1.SetAttribute("month", "12");
xmlelem1.SetAttribute("day", "8");
xmlelem.AppendChild(xmlelem1);
XmlElement xmlelem2 = xmldoc.CreateElement("", "To", "");
XmlText xmlText = xmldoc.CreateTextNode("黄建国");
xmlelem2.AppendChild(xmlText);
xmlelem1.AppendChild(xmlelem2); XmlElement xmlelem3 = xmldoc.CreateElement("", "From", "");
xmlText = xmldoc.CreateTextNode("李忠");
xmlelem3.AppendChild(xmlText);
xmlelem1.AppendChild(xmlelem3); XmlElement xmlelem4 = xmldoc.CreateElement("", "Heading", "");
xmlText = xmldoc.CreateTextNode("提醒");
xmlelem4.AppendChild(xmlText);
xmlelem1.AppendChild(xmlelem4); XmlElement xmlelem5 = xmldoc.CreateElement("", "Message", "");
xmlText = xmldoc.CreateTextNode("不要忘了这个周末和我的约会");
xmlelem5.AppendChild(xmlText);
xmlelem1.AppendChild(xmlelem5); xmldoc.Save(XmlFile);
}
<Day year="2009" month="12" day="8">
<To>黄建国</To>
<From>李忠</From>
<Heading>提醒</Heading>
<Message>不要忘了这个周末和我的约会</Message>
</Day>
的子节点