微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Linq to XML将XElement添加到XML文件

使用Linq到XML,我试图添加一个XElement到现有的XML文件。 它必须在Windows Phone .NET框架中完成。 目前我的XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?> <Kids> <Child> <Name>Kid1</Name> <FirstName>hisname</FirstName> </Child> </Kids> and my code looks like this: using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",fileMode,store)) { XDocument x = XDocument.Load(stream); XElement t = new XElement("Child",new XElement("Name",pName),new XElement("FirstName",pFirstname)); t.Add(from el in x.Elements() where el == el.Descendants("Child").Last() select el); x.Save(stream); } this doesn't do what I want to achieve. I want to add a new "Child" element to the the exisiting XML file like this : <?xml version="1.0" encoding="utf-8"?> <Kids> <Child> <Name>Kid1</Name> <FirstName>hisname</FirstName> </Child> <Child> <Name>kid2</Name> <FirstName>SomeName</FirstName> </Child> </Kids> Could use some help because I am stuck ;-) After the tips from GSerjo,my code looks like this Now: try { if (store.FileExists("YourKids.xml")) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",FileMode.Open,store)) { var x = XElement.Load(stream); var t = new XElement("Child",pFirstname) ); x.Add(t); x.Save(stream); stream.Close(); return; } } else { using (IsolatedStorageFileStream CreateIsf = new IsolatedStorageFileStream("YourKids.xml",FileMode.Create,store)) { var xDoc = new XElement("Kids",new XElement("Child",pFirstname) ) ); xDoc.Save(CreateIsf); CreateIsf.Close(); return; } } } catch (Exception ex) { message = ex.Message; }

这给了我一个这样的XML文件

<?xml version="1.0" encoding="utf-8"?> <Kids> <Child> <Name>test</Name> <FirstName>test</FirstName> </Child> </Kids><?xml version="1.0" encoding="utf-8"?> <Kids> <Child> <Name>test</Name> <FirstName>test</FirstName> </Child> <Child> <Name>testing</Name> <FirstName>testing</FirstName> </Child> </Kids>

一个仍然不正确,有什么想法的人?

Windows 8应用程序中的屏幕分辨率检测

system.Reflection.Assembly.GetExecutingAssembly()。FullName。的等价物

为什么我们需要SDK和.NET Framework?

Silverlight:编码一个webClientstream

禁用WP7的数据透视

寻找集成WEB应用程序与Silverlight和VB6的客户端集成解决scheme

提交WP7应用程序的试用版

使用Silverlight 3创buildWPF应用程序跨平台(Windows + OS X)

地图点击事件/鼠标按下事件在Windows Phone 8?

如何将string的string转换为字符?

初始的xml文件

<?xml version="1.0" encoding="utf-8"?> <Kids> <Child> <Name>Kid1</Name> <FirstName>hisname</FirstName> </Child> </Kids>

下面的代码添加一个新的孩子到现有的XML

[Test] public void test() { string filPath = @"YourKids.xml"; var root = XElement.Load(filPath); var newChild = new XElement("Child","NewName"),"NewFirstName")); root.Add(newChild); root.Save(filPath); }

结果xml文件

<?xml version="1.0" encoding="utf-8"?> <Kids> <Child> <Name>Kid1</Name> <FirstName>hisname</FirstName> </Child> <Child> <Name>NewName</Name> <FirstName>NewFirstName</FirstName> </Child> </Kids>

更新

保存错误,你应该设置流长度为0

说明

读取器现有文件后, 流不会删除任何数据

using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",store)) { var x = XElement.Load(stream);

所以当你打电话时,数据已被追加

x.Save(stream); stream.Close();

添加stream.SetLength(0); 之前x.Save(流); 所有的数据将被覆盖。

这里是完整版本

if (store.FileExists("YourKids.xml")) { using (var stream = new IsolatedStorageFileStream("YourKids.xml",pFirstname) ); x.Add(t); stream.SetLength(0); x.Save(stream); stream.Close(); } } else { using (var CreateIsf = new IsolatedStorageFileStream("YourKids.xml",pFirstname) ) ); xDoc.Save(CreateIsf); CreateIsf.Close(); } }

请注意:我删除了返回语句是无用的。

PS看看resharper,它可以改善代码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐