[英]c# LINQ-to-XML update (save) list of elements in file after ordering
I have an XML file as such: 我有一个XML文件:
<?xml version="1.0" encoding="utf-8"?>
<FooMgr>
<BlargDate>2017-03-06 10:26:21</BlargDate>
<Bars>
<Bar>
<BarId>222</BarId>
<BarVal>QWERTY</BarVal>
</Bar>
<Bar>
<BarId>77</BarId>
<BarVal>DVORAK</BarVal>
</Bar>
<Bar>
<BarId>9999</BarId>
<BarVal>AZERTY</BarVal>
</Bar>
</Bars>
</FooMgr>
and I am: 我是:
although the added element is in the list after I save it, it does not keep the order I defined in code when saving. 虽然添加的元素在我保存之后位于列表中,但它在保存时不保留我在代码中定义的顺序。 Here's what i go so far (mostly working valid code)
这是我到目前为止(主要是工作有效代码)
//read in the xml file
XDocument doc = XDocument.Load(...);
//add a new 'Bar' element
XElement bar1 = new XElement("Bar",
new XElement("BarId", 101),
new XElement("BarVal", "HCESAR"));
doc.Element("FooMgr").Element("Bars").Add(bar1);
//sort descending by BarId
IEnumerable<XElement> allBars = doc.Descendants("FooMgr")
.Select(x => x.Element("Bars"))
.Descendants("Bar")
.ToArray();
allBars = allBars.OrderByDescending(s => int.Parse(s.Element("BarId").Value));
//save file
doc.Save(...);
// note: at this point the file successfully saves (along with the
// new 'bar' value, but the order that is set for allBars does not
// make it back into the file.
although this line: 虽然这行:
allBars = allBars.OrderByDescending(s => int.Parse(s.Element("BarId").Value));
does seem to properly sort the 'Bars' elements in code, when I save it back to the file the order is not persisted. 似乎正确地对代码中的'Bars'元素进行排序,当我将其保存回文件时,订单不会保留。
Any thoughts? 有什么想法吗?
You can do this before you call Save
method: 您可以在调用
Save
方法之前执行此操作:
//Remove bar elements from your doc
doc.Element("FooMgr").Element("Bars").Elements("Bar").Remove();
//Add the ordered bar nodes
doc.Element("FooMgr").Element("Bars").Add(allBars);
//save file
doc.Save(...);
More info about Remove
extension method and Add
method 有关
Remove
扩展方法和Add
方法的更多信息
First of all, your xml is not valid, i gues the right one will be: 首先,你的xml无效,我猜对了:
<?xml version="1.0" encoding="utf-8"?>
<FooMgr>
<BlargDate>2017-03-06 10:26:21</BlargDate>
<Bars>
<Bar>
<BarId>222</BarId>
<BarVal>QWERTY</BarVal>
</Bar>
<Bar>
<BarId>77</BarId>
<BarVal>DVORAK</BarVal>
</Bar>
<Bar>
<BarId>9999</BarId>
<BarVal>AZERTY</BarVal>
</Bar>
</Bars>
</FooMgr>
Your allBars is just a collection of nodes it doesn't belongs to any node, so you need to hang it under Bars, i would suggest first remove all items there: 你的allBars只是一个节点的集合,它不属于任何节点,所以你需要将它挂在Bars下,我建议先删除那里的所有项目:
XElement bar1 = new XElement("Bar",
new XElement("BarId", 101),
new XElement("BarVal", "HCESAR"));
doc.Element("FooMgr").Element("Bars").Add(bar1);
//sort descending by BarId
IEnumerable<XElement> allBars = doc.Descendants("FooMgr")
.Select(x => x.Element("Bars"))
.Descendants("Bar")
.ToArray();
allBars = allBars.OrderByDescending(s => int.Parse(s.Element("BarId").Value));
doc.Element("FooMgr").Element("Bars").RemoveAll();
doc.Element("FooMgr").Element("Bars").Add(allBars);
In VB this would be 在VB中这将是
Dim doc As XElement
' to load from a file
'xe = XElement.Load(yourpath)
' for testing
doc = <FooMgr>
<BlargDate>2017-03-06 10:26:21</BlargDate>
<Bars>
<Bar>
<BarId>222</BarId>
<BarVal>QWERTY</BarVal>
</Bar>
<Bar>
<BarId>77</BarId>
<BarVal>DVORAK</BarVal>
</Bar>
<Bar>
<BarId>9999</BarId>
<BarVal>AZERTY</BarVal>
</Bar>
</Bars>
</FooMgr>
doc.<Bars>.LastOrDefault.Add(<Bar>
<BarId>101</BarId>
<BarVal>HCESAR</BarVal>
</Bar>)
Dim ie As List(Of XElement) = doc.<Bars>.Elements.OrderBy(Function(el) Integer.Parse(el.<BarId>.Value)).ToList
doc.<Bars>.Elements.Remove()
doc.<Bars>.FirstOrDefault.Add(ie)
' to save file
' doc.Save(yourpath)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.