我有一个接收以下XML的应用程序,我必须添加一些额外的信息(新元素)。你能帮我理解怎么做吗?
<Feed>
<Claims>
<Claim>
<ClaimID>123</ClaimID>
<Reference>245</Reference>
<AccidentDetails>
<IncidentDate>2015-08-05</IncidentDate>
</AccidentDetails>
<DriverDetails>
<DriverFirstName>Text</DriverFirstName>
<DriverLastName>Text</DriverLastName>
</DriverDetails>
<ClientVehicleDetails>
<VehicleLegallyDriveable>Yes</VehicleLegallyDriveable>
<VehicleLocation>In Use</VehicleLocation>
</ClientVehicleDetails>
</Claim>
</Claims>
</Feed>
但我需要加载XML并添加如下所示的部分
<Feed>
//This is the section I need to add to my XML
<Control>
<Username>Test</Username>
<Password>TestPass</Password>
</Control>
//The following XML will remain the same
<Claims>
<Claim>
<ClaimID>123</ClaimID>
<Reference>245</Reference>
<AccidentDetails>
<IncidentDate>2015-08-05</IncidentDate>
</AccidentDetails>
<DriverDetails>
<DriverFirstName>Text</DriverFirstName>
<DriverLastName>Text</DriverLastName>
</DriverDetails>
<ClientVehicleDetails>
<VehicleLegallyDriveable>Yes</VehicleLegallyDriveable>
<VehicleLocation>In Use</VehicleLocation>
</ClientVehicleDetails>
</Claim>
</Claims>
</Feed>
答案 0 :(得分:1)
您可以使用LINQ-to-XML
轻松完成此操作: -
XDocument xdoc = XDocument.Load("ClaimXMLFile");
XDocument xdoc2 = new XDocument(new XElement("Feed",
new XElement("Control",
new XElement("Username", "TestPass"),
new XElement("Password", "Test")),
xdoc.Root));
xdoc2.Save(@"NewXMLFileName");