我有这样的XML:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<!--Alert notes-->
<Aletrs>
<Alert>
<Name>Counter</Name>
<Value>0</Value>
</Alert>
<Alert>
<ID>1</ID>
<Name>02:46:10 - Alert ID.1 nr.1 Camera1 - photo</Name>
<Value>Capture/Images/Camera1_snapshoot_nr1-2014-03-02_02-46-10.png</Value>
</Alert>
<Alert>
<ID>1</ID>
<Name>02:46:11 - Alert ID.1 nr.2 Camera1 - photo</Name>
<Value>Capture/Images/Camera1_snapshoot_nr2-2014-03-02_02-46-11.png</Value>
</Alert>
</Alerts>
首先,我需要帮助修改Counter
名称Alert节点中的Value中的数字,我得到了代码如何获取此值,但我无法将其修改为更改值并保存修改后的XML:
string index = (from xml2 in dailyXML.Descendants("Alert")
where xml2.Element("Name").Value == "Counter"
select xml2.Element("Value").Value).FirstOrDefault();
此外,我需要帮助代码删除< ID>..< /ID>
答案 0 :(得分:0)
var xDoc = XDocument.Load(filename);
/*1*/
var value = xDoc.Descendants("Alert")
.FirstOrDefault(a => a.Element("Name").Value == "Counter")
.Element("Value");
value.Value = "1";
/*2*/
foreach(var alert in xDoc.Descendants("Alert")
.GroupBy(a => (string)a.Element("ID")))
{
alert.Skip(1).ToList().ForEach(x => x.Remove());
}