Silverlight独立存储空间的问题求大神

huzhong_vip 2013-06-06 07:57:40
开发环境:Silverlight4
思路:一个Silverlight程序,功能是,有3个按钮,分别实现,在独立存储空间中创建XML,读取独立存储空间中XML,写入XML。
创建XML按钮代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
XElement imgBox = new XElement("imgBox", new XElement("img", new XElement("source", "asdsad"), new XElement("width", "150"), new XElement("height", 150)));

XDocument imgXml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XComment("这是一个XML文件"), imgBox);

using (IsolatedStorageFile userFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("imgXml.xml", FileMode.Create, userFile))
{
imgXml.Save(fileStream);
fileStream.Close();
MessageBox.Show("chenggong");
}
}
}
catch(IsolatedStorageException ex)
{
MessageBox.Show(ex.ToString());
}

}


读取XML按钮代码:
private void button3_Click(object sender, RoutedEventArgs e)
{
try
{
using (IsolatedStorageFile userfile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream filestream = userfile.OpenFile("imgXml.xml", FileMode.Open, FileAccess.Read))
{
StreamReader str = new StreamReader(filestream);
TextBox textbox = new TextBox();
textbox.Text = str.ReadToEnd().ToString();
textbox.Height = 350;
this.LayoutRoot.Children.Add(textbox);
str.Close();
}
}
}


写入XML按钮代码:
private void button4_Click(object sender, RoutedEventArgs e)
{
XDocument imgXml = null;
try
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{

using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("imgXml.xml", FileMode.Open, file))
{
imgXml =XDocument.Load(fileStream);

XElement new1 = new XElement("img", new XElement("source", "11111"), new XElement("width", "222"), new XElement("height", "555"));

imgXml.Root.Add(new1);
imgXml.Save(fileStream);
fileStream.Close();
MessageBox.Show("添加成功");
}
}
}
catch (IsolatedStorageException ex)
{
MessageBox.Show(ex.ToString());
}
}


问题:点击创建按钮后再点击读取XML按钮能正常创建和读取XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--这是一个XML文件-->
<imgBox>
<img>
<source>asdsad</source>
<width>150</width>
<height>150</height>
</img>
</imgBox>


但是点击写入XML按钮后,XML就变成了这样,这是怎么回事,怎么解决?
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--这是一个XML文件-->
<imgBox>
<img>
<source>asdsad</source>
<width>150</width>
<height>150</height>
</img>
</imgBox><?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--这是一个XML文件-->
<imgBox>
<img>
<source>asdsad</source>
<width>150</width>
<height>150</height>
</img>
<img>
<source>11111</source>
<width>222</width>
<height>555</height>
</img>
</imgBox>


我想要的效果是点击写入XML按钮后在XML文件中再添加元素,效果是这样的
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--这是一个XML文件-->
<imgBox>
<img>
<source>asdsad</source>
<width>150</width>
<height>150</height>
</img>
<img>
<source>11111</source>
<width>222</width>
<height>555</height>
</img>
</imgBox>
...全文
79 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
huzhong_vip 2013-06-06
  • 打赏
  • 举报
回复
搞定了。。。不应该把imgXml.Save(fileStream);放到using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("imgXml.xml", FileMode.Open, file)) 中

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧