使用 Linq to XML 将 XElement 添加到 XML 文件 [英] Adding XElement to XML file using Linq to XML
问题描述
使用 Linq to XML,我尝试将 XElement 添加到现有 XML 文件.它必须在 Windows Phone .NET 框架中完成.目前我的 XML 文件如下所示:
Using Linq to XML, I am trying to add an XElement to an existing XML file. It has to be done in the Windows Phone .NET framework. Currently my XML file looks like this:
<?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",
new XElement("Name", pName),
new XElement("FirstName", 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",
new XElement("Name", pName),
new XElement("FirstName", pFirstname)
)
);
xDoc.Save(CreateIsf);
CreateIsf.Close();
return;
}
}
}
catch (Exception ex)
{
message = ex.Message;
}
这给了我一个这样的 xml 文件:
Which gives me an xml file like this:
<?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>
哪个仍然不正确,有人有任何想法吗?
Which is still not correct, any ideas anyone ?
推荐答案
初始 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<Kids>
<Child>
<Name>Kid1</Name>
<FirstName>hisname</FirstName>
</Child>
</Kids>
以下代码向现有 xml 添加一个新子项
Following code add one new child to existing xml
[Test]
public void Test()
{
string filPath = @"YourKids.xml";
var root = XElement.Load(filPath);
var newChild = new XElement("Child",
new XElement("Name", "NewName"),
new XElement("FirstName", "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
Bug on save, you should set stream length to 0
说明
读取现有文件后,stream不会删除任何数据
After reader existing file, stream does not remove any data
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",FileMode.Open, store))
{
var x = XElement.Load(stream);
所以当您调用时,数据已附加
So when you call, data has been appended
x.Save(stream);
stream.Close();
在x.Save(stream);之前添加stream.SetLength(0);,所有数据将被覆盖.
Add stream.SetLength(0); before x.Save(stream); and all data will be overwritten.
这里是完整版
if (store.FileExists("YourKids.xml"))
{
using (var stream = new IsolatedStorageFileStream("YourKids.xml", FileMode.Open,
store))
{
var x = XElement.Load(stream);
var t = new XElement("Child",
new XElement("Name", pName),
new XElement("FirstName", pFirstname)
);
x.Add(t);
stream.SetLength(0);
x.Save(stream);
stream.Close();
}
}
else
{
using (var CreateIsf = new IsolatedStorageFileStream("YourKids.xml", FileMode.Create, store))
{
var xDoc = new XElement("Kids",
new XElement("Child",
new XElement("Name", pName),
new XElement("FirstName", pFirstname)
)
);
xDoc.Save(CreateIsf);
CreateIsf.Close();
}
}
请注意:我已将 return 语句删除为无用.
Please note: I've removed return statements as useless.
附言看看resharper,它可以改进代码.
P.S. Take a look on resharper, it can improve code.
这篇关于使用 Linq to XML 将 XElement 添加到 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!