具有键/值对的新XML文档

时间:2015-07-08 18:02:28

标签: c# xml xmldocument

我对编程很新,我想通过xml创建一个新的键值对文档发送到Web服务

<BuildAddOns>
    <UserInput Question="foo" Answer="blahh" Type="String" />
    <UserInput Question="foo" Answer="blahh" Type="String" />
</BuildAddOns>

1 个答案:

答案 0 :(得分:0)

您可以使用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");