为什么重写XML文件创建一个额外的结束标记? [英] Why does overwriting an XML file create an extra end tag?
本文介绍了为什么重写XML文件创建一个额外的结束标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建在C#中,有写一些用户设置到一个XML文件的应用程序。他们读完全正常,但是当我试图把它们写回他们创造一个额外的结束标记,该程序无法读取。
XML文件:
< XML版本=1.0编码=UTF-8&GT?;
<选项>
<全屏>假< /全屏>
< resolutionX> 1280< / resolutionX>
< resolutionY> 720< / resolutionY>
< VSYNC>真< / VSYNC>
&其中; AA→2&其中; / AA>
&其中; musicvolume> 0℃/ musicvolume>
&其中; soundvolume> 0℃/ soundvolume>
< /选项>
code的写道:
的FileStream流=
新的FileStream(的configs / options.xml,FileMode.Open,FileAccess.ReadWrite);
XmlDocument的文档=新的XmlDocument();
doc.Load(流);
stream.Seek(0,SeekOrigin.Begin);
doc.SelectSingleNode(/选项/全屏)的InnerText = fullscreen.ToString()。
doc.SelectSingleNode(/选项/垂直同步)的InnerText = vsync.ToString()。
doc.SelectSingleNode(/选项/ resolutionX)的InnerText = resolutionX.ToString()。
doc.SelectSingleNode(/选项/ resolutionY)的InnerText = resolutionY.ToString()。
doc.SelectSingleNode(/选项/ AA)的InnerText = aa.ToString()。
doc.SelectSingleNode(/选项/ musicvolume)的InnerText = musicvolume.ToString()。
doc.SelectSingleNode(/选项/ soundvolume)的InnerText = soundvolume.ToString()。
doc.Save(流);
stream.Close();
我结束了有什么用:
< XML版本=1.0编码=UTF-8&GT?;
<选项>
<全屏>真< /全屏>
< resolutionX> 1280< / resolutionX>
< resolutionY> 720< / resolutionY>
< VSYNC>真< / VSYNC>
&其中; AA> 4℃/ AA>
&其中; musicvolume> 0℃/ musicvolume>
&其中; soundvolume> 0℃/ soundvolume>
< /选项> /选项>
解决方案
既然你写相同的流,如果修改后的XML比原来的短,差异将继续存在。您可以使用 FileStream.SetLength
保存到修复后:
doc.Save(流);
stream.SetLength(stream.Position);
stream.Close();
I am creating an application in C# that has to write some user settings to an XML file. They are read perfectly fine, but when I try to write them back they create an extra end tag that the program cannot read.
XML file:
<?xml version="1.0" encoding="utf-8" ?>
<options>
<fullscreen>False</fullscreen>
<resolutionX>1280</resolutionX>
<resolutionY>720</resolutionY>
<vsync>True</vsync>
<AA>2</AA>
<musicvolume>0</musicvolume>
<soundvolume>0</soundvolume>
</options>
Code that writes:
FileStream stream =
new FileStream("configs/options.xml", FileMode.Open, FileAccess.ReadWrite);
XmlDocument doc = new XmlDocument();
doc.Load(stream);
stream.Seek(0, SeekOrigin.Begin);
doc.SelectSingleNode("/options/fullscreen").InnerText = fullscreen.ToString();
doc.SelectSingleNode("/options/vsync").InnerText = vsync.ToString();
doc.SelectSingleNode("/options/resolutionX").InnerText = resolutionX.ToString();
doc.SelectSingleNode("/options/resolutionY").InnerText = resolutionY.ToString();
doc.SelectSingleNode("/options/AA").InnerText = aa.ToString();
doc.SelectSingleNode("/options/musicvolume").InnerText = musicvolume.ToString();
doc.SelectSingleNode("/options/soundvolume").InnerText = soundvolume.ToString();
doc.Save(stream);
stream.Close();
What I end up with:
<?xml version="1.0" encoding="utf-8" ?>
<options>
<fullscreen>True</fullscreen>
<resolutionX>1280</resolutionX>
<resolutionY>720</resolutionY>
<vsync>True</vsync>
<AA>4</AA>
<musicvolume>0</musicvolume>
<soundvolume>0</soundvolume>
</options>/options>
解决方案
Since you’re writing to the same stream, if the modified XML is shorter than the original, the difference will remain. You can use FileStream.SetLength
after saving to fix that:
doc.Save(stream);
stream.SetLength(stream.Position);
stream.Close();
这篇关于为什么重写XML文件创建一个额外的结束标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文