Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
附加新的XElement会将整个XML添加到流中的现有XML中_Xml_Linq_Linq To Xml_Xelement_Isolatedstoragefile - Fatal编程技术网

附加新的XElement会将整个XML添加到流中的现有XML中

附加新的XElement会将整个XML添加到流中的现有XML中,xml,linq,linq-to-xml,xelement,isolatedstoragefile,Xml,Linq,Linq To Xml,Xelement,Isolatedstoragefile,我在InternalFielStorage中存储了一个现有XML,如 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Root> <Books> <Author name="Sam" /> </Books> </Root> 我试图在“Author”节点下添加一个“title”节点,但保存后,我看到一个全新的xml添加到现有xml中

我在InternalFielStorage中存储了一个现有XML,如

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Books>
        <Author name="Sam" />
  </Books>
</Root>

我试图在“Author”节点下添加一个“title”节点,但保存后,我看到一个全新的xml添加到现有xml中,如

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
    <Books>
        <Author name="Sam" />
    </Books>
</Root>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>  
    <Books>
        <Author name="Sam" />        
        <Title>Test</Title>
    </Books>
</Root>

试验
我正在为此使用的代码

using (IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream(App.FileName, FileMode.Open, FileAccess.ReadWrite, myStore)) { XDocument _xDoc = XDocument.Load(myStream); XElement srcTree = new XElement("Title", "test"); _xDoc.Element("Root").Element("Books").Add(new XElement(srcTree)); _xDoc.Save(myStream); 使用(IsolatedStorageFile myStore=IsolatedStorageFile.GetUserStoreForApplication()) { 使用(IsolatedStorageFileStream myStream=新的IsolatedStorageFileStream(App.FileName,FileMode.Open,FileAccess.ReadWrite,myStore)) { XDocument xDoc=XDocument.Load(myStream); XElement srcTree=新XElement(“标题”、“测试”); _xDoc.Element(“Root”).Element(“Books”).Add(new-XElement(srcTree)); _xDoc.Save(myStream); 问题:
1.如何避免将新XML追加到现有XML?

2.加载流时,如何使“title”标记位于下,位置设置为文件的最后一个字节-保存文件前需要重置位置

用这个

myStream.Position = 0;
_xDoc.Save(myStream);
请参阅文档