【发布时间】:2016-04-14 10:14:15
【问题描述】:
我正在使用 Visual Studio 2015 在 windows 10 上开发一个通用 windows 应用程序,并且有一个相当大的 Xml 结构如下:
<header id = "1">
<title>
some text
</title>
<question>
a question
</question>
<user_input>
<input1>
</input1>
<input2>
</input2>
</user_input>
</header>
<header id = "2">
<title>
some text
</title>
<question>
a question
</question>
<user_input>
<input1>
</input1>
<input2>
</input2>
</user_input>
</header>
...
这重复了很多次。有些部分永远不应更改(例如标题、问题)。现在我想将新元素写入“ui”,以便可以再次读取并在 texbox 中显示新内容。 我使用 FileStream 和 XmlDocument 和 XmlNodeList 来读取 Xml 并在文本块上显示内容:
path = "test.xml";
FileStream stream = new Filestream(path, FileMode.Open, FileAcces.Read);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(reader);
XmlNodeList node = xdoc.GetElementsByTagName("header");
textblock1.Text = node[0].Attributes["id"].Value;
textblock2.Text = node[i].ChildNode[1].InnerText;
....
我尝试将此写入 Xml:
XDocument xdoc = XDocument.Load(path);
XElement ele = xdoc.Element("header");
ele.Add(new XElement("user_input",
new XElement("input1", newtext)));
xdoc.Save(path); <---- at this point there is an error
“参数 1:无法从 'string' 转换为 'System.IO.Stream'”
我的问题是:如何将用户输入(一些字符串)写入我想要的位置?第一个输入应写入 id = 1 的 header 到 user_input,第二个输入到 header id = "2" 等等。我已经尝试使用 XDocument 加载 xml 并使用 XElement 编写一个新元素,但它完全可以工作。我的 xml 有问题吗?还是它的功能?提前谢谢你。
【问题讨论】:
标签: c# xml linq-to-xml win-universal-app