带有键/值对的新XML文档
本文关键字:XML 文档 | 更新日期: 2023-09-27 18:01:45
我对编程相当陌生,我想通过xml创建一个新的键值对文档发送到web服务
<BuildAddOns>
<UserInput Question="foo" Answer="blahh" Type="String" />
<UserInput Question="foo" Answer="blahh" Type="String" />
</BuildAddOns>
可以使用XDocument
读写xml文件。下面是代码:
XElement root = new XElement("BuildAddOns");
XDocument doc = new XDocument(root);
XElement element1 = new XElement("UserInput");
element1.SetAttributeValue("Question", "foo");
element1.SetAttributeValue("Answer", "blahh");
element1.SetAttributeValue("Type", "String");
doc.Root.Add(element1);
XElement element2 = new XElement("UserInput");
element2.SetAttributeValue("Question", "foo");
element2.SetAttributeValue("Answer", "blahh");
element2.SetAttributeValue("Type", "String");
doc.Root.Add(element2);
doc.Save(@"C:'New Text Document.txt");