C# 如何在C中更新XML文件#

C# 如何在C中更新XML文件#,c#,xmldocument,C#,Xmldocument,我有一个XML文件。我想创建一个新节点,并将其附加到XML文件的末尾,然后将其保存回内存 <IntCal> <User> <Date>12/09/2012</Date> <Client>abcd</Client> <Jewellery>Others</Jewellery> <ROI>7.5</ROI> <Description&

我有一个XML文件。我想创建一个新节点,并将其附加到XML文件的末尾,然后将其保存回内存

<IntCal>
  <User>
    <Date>12/09/2012</Date>
    <Client>abcd</Client>
    <Jewellery>Others</Jewellery>
    <ROI>7.5</ROI>
    <Description>Some Description</Description>
  </User>
<IntCal>
谢谢参考:

如果你想走LINQ路线,你可以:

XDocument xDoc = XDocument.Load(xmlFile);
xDoc.Element("IntCal")
    .Add(new XElement("User"));
就我个人而言,我会选择XDocument并使用LINQ,但这两种方法都有效。
参考资料:

参考资料:

如果你想走LINQ路线,你可以:

XDocument xDoc = XDocument.Load(xmlFile);
xDoc.Element("IntCal")
    .Add(new XElement("User"));
就我个人而言,我会选择XDocument并使用LINQ,但这两种方法都有效。
参考资料:

XmlDocument是一门古老的学科,为什么不使用XDocument,简单易懂:

XDocument xDoc = XDocument.Load(xmlFile);
xDoc.Root.Add(new XElement("User", 
                  new XElement("Client", "John"), 
                  new XElement("Jewellery", "Others")));
xDoc.Save(xmlFile);
参考资料:

  • XmlDocument是一个古老的流派,为什么不使用XDocument,简单易用:

    XDocument xDoc = XDocument.Load(xmlFile);
    xDoc.Root.Add(new XElement("User", 
                      new XElement("Client", "John"), 
                      new XElement("Jewellery", "Others")));
    xDoc.Save(xmlFile);
    
    参考资料:


  • 有点离题。。。但是,使用LINQ到XML的解决方案通常更容易一些。。。但是使用LINQ到XML的解决方案通常要容易得多