[英]How to Edit XML node with same name using XDocument in C#
This is my XML 这是我的XML
<MilestoneCollection>
<Milestone>
<Description>Departure from Origin</Description>
<EventCode>DEP</EventCode>
<Sequence>1</Sequence>
<ActualDate></ActualDate>
<ConditionReference>LOC=<FirstLeg.Origin></ConditionReference>
<ConditionType>RFP</ConditionType>
<EstimatedDate>2015-05-10T18:07:00</EstimatedDate>
</Milestone>
<Milestone>
<Description>Departure from Origin</Description>
<EventCode>DEP</EventCode>
<Sequence>1</Sequence>
<ActualDate></ActualDate>
<ConditionReference>LOC=<FirstLeg.Origin></ConditionReference>
<ConditionType>RFP</ConditionType>
<EstimatedDate>2015-05-10T18:07:00</EstimatedDate>
</Milestone>
<Milestone>
<Description>Arrival at Destination</Description>
<EventCode>ARV</EventCode>
<Sequence>2</Sequence>
<ActualDate></ActualDate>
<ConditionReference>LOC=<LastLeg.Destination></ConditionReference>
<ConditionType>RFP</ConditionType>
<EstimatedDate>2015-05-11T14:02:00</EstimatedDate>
</Milestone>
<Milestone>
<Description>Arrival at Destination</Description>
<EventCode>ARV</EventCode>
<Sequence>2</Sequence>
<ActualDate></ActualDate>
<ConditionReference>LOC=<LastLeg.Destination></ConditionReference>
<ConditionType>RFP</ConditionType>
<EstimatedDate></EstimatedDate>
</Milestone>
</MilestoneCollection>
This is my current code. 这是我当前的代码。 I need to Edit the 1st, 2nd, or 3rd Milestone.
我需要编辑第一,第二或第三里程碑。 I don't know how exactly the syntax for that.
我不知道该怎么语法。 How do i edit element Milestone[0] something like this.
我如何编辑元素Milestone [0]这样的东西。
XDocument doc = XDocument.Load(sample.xml);
var xmldocu = doc.Descendants("MilestoneCollection");
You can do something like this 你可以做这样的事情
XDocument doc = XDocument.Load(sample.xml);
doc.Descendants("Milestone").First()
.Descendants("EstimatedDate").First().Value = DateTime.Now.ToString();
To update Nth element 更新第N个元素
doc.Descendants("Milestone")
.ElementAt(2).Descendants("EstimatedDate").First().Value = DateTime.Now.ToString();
Update This will update the second Milestone 更新这将更新第二个里程碑
doc.Elements("MilestoneCollection")
.Elements("Milestone")
.ElementAt(1)
.Descendants("EstimatedDate")
.First()
.Value=DateTime.Now.ToString();
var xmldocu = xml.Descendants("Milestone").ToArray();
for (var i = 0; i < 3 && i < xmldocu.Length; i++)
{
var ele = xmldocu[i];
ele.Element("EstimatedDate").SetValue(DateTime.Now);
}
I need to Edit the 1st, 2nd, or 3rd Milestone
我需要编辑第一,第二或第三里程碑
loop from 1 to 3? 从1循环到3?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.