我想将 xml 文件读出到文本 block 中,用户可以在文本 block 中编辑此文本并将附加更改应用回 xml 文件...我目前被卡住了。这是我到目前为止所做的:
private void Editbuildstreams_Click(object sender, RoutedEventArgs e)
{
BuildstreamTextblock.Visibility = Visibility.Visible;
using (StreamReader srr = new StreamReader("initial.xml"))
{
string line;
while ((line = srr.ReadLine()) != null)
{
BuildstreamTextblock.Text = line;
}
}
}
这是xml文件的结构:
<xml>
<email>
..
</email>
<buildstream1>
<path value="apple"/>
</buildstream1>
<buildstream2>
<path value="pear"/>
<path value="bananas"/>
</buildstream2>
</xml>
问题是:
我如何读出 xml 文件的行?
我只对获取 xml 文件的某些部分感兴趣。我如何只获取 <buildstream1>
& <buildstream2>
被读出?
最佳答案
是这样的吗?
var originalDocument = XDocument.Load(fileName);
var originalElement = originalDocument.XPathSelectElement("xml/buildstream1");
textBox.Text = originalElement.ToString();
// do changes in the text box
var newDocument = XDocument.Parse(textBox.Text);
var newElement = newDocument.Elements().Single();
// insert edited element
originalElement.AddAfterSelf(newElement);
// remove original element
originalElement.Remove();
originalDocument.Save(fileName);
// or
var resultXml = originalDocument.ToString();
关于c# - streamreader 读出 xml 文件的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6339921/