C# LINQ to XML新添加的元素未保存到XML文件

C# LINQ to XML新添加的元素未保存到XML文件,c#,xml,linq,C#,Xml,Linq,我正在尝试更新XML文件,但新添加的XElement未保存到XML文件中。当我使用调试器时,我可以看到新的XElement被添加到节点的列表中,但在XML中它不在那里 相关XML <object occur="-1" prefix="Pages" table="Pages" description="Title" singlename="Pagina" name="Pagina's" statemode="disabled" link="" exportable="0"> <

我正在尝试更新XML文件,但新添加的XElement未保存到XML文件中。当我使用调试器时,我可以看到新的XElement被添加到节点的列表中,但在XML中它不在那里

相关XML

<object occur="-1" prefix="Pages" table="Pages" description="Title" singlename="Pagina" name="Pagina's" statemode="disabled" link="" exportable="0">
  <views>
    <view id="1" type="tree" name="Subpagina Overzicht">
      <sql></sql>
      <model table="Pages" parent="parentID" />
      <preview textlength="50" />
    </view>
    <view id="2" type="normal" name="Overzicht">
      <sql></sql>
      <columns>
        <column description="Naam" sortnode="Title" width="" />
      </columns>
      <values></values>
    </view>
  </views>
  <objectviews>
    <view id="view1" type="view" name="Bekijken" show="1" link=""></view>
    <view id="view2" type="edit" name="Bewerken" show="1" link=""></view>
    <view id="view3" type="delete" name="Verwijderen" show="1" link=""></view>
    <view id="view4" type="add" name="Toevoegen" show="1" link=""></view>
  </objectviews>
  <nodes>
    <node id="ParentId" description="Bovenliggende pagina" required="0" datatype="i" nodetype="-2" fieldlength="" reference="Pages,Title,id" exclude="-" tab="General" order="10" default="" decimals="" maxchars="" help="" unique="" show="1" />
    <node id="Title" description="Titel" required="0" datatype="s" nodetype="" fieldlength="" reference="" exclude="" tab="General" order="10" default="" decimals="" maxchars="" help="" unique="" show="1" />
    <node id="UrlTitle" description="Url" required="0" datatype="s" nodetype="" fieldlength="" reference="" exclude="" tab="General" order="30" default="" decimals="" maxchars="" help="Leeg laten zorgt voor een automatische url, gebaseerd op Naam" unique="" show="0" />
    <node id="Content" description="Inhoud" required="0" datatype="s" nodetype="-3" fieldlength="" reference="" exclude="" tab="General" order="40" default="" decimals="" maxchars="" help="" unique="" show="1" />
    <node id="Sorting" description="Sorting" required="0" datatype="i" nodetype="-6" fieldlength="" reference="" exclude="" tab="General" order="100" default="" decimals="" maxchars="" help="" unique="" show="0" />
    <node id="SeoTitle" description="Pagina titel" required="0" datatype="s" nodetype="" fieldlength="" reference="" exclude="" tab="Seo" order="10" default="" decimals="" maxchars="" help="" unique="" show="1" />
    <node id="SeoDescription" description="Description" required="0" datatype="s" nodetype="" fieldlength="" reference="" exclude="" tab="Seo" order="20" default="" decimals="" maxchars="" help="" unique="" show="1" />
    <node id="SeoKeywords" description="Keywords" required="0" datatype="s" nodetype="" fieldlength="" reference="" exclude="" tab="Seo" order="30" default="" decimals="" maxchars="" help="" unique="" show="1" />
  </nodes>
  <tabs>
    <tab id="General" order="10" type="normal" child="" foreignkey="" description="Algemeen" link="" />
    <tab id="Seo" order="20" type="normal" child="" foreignkey="" description="SEO" link="" />
    <tab id="Blocks" order="30" type="child" child="ContentBlocks" foreignkey="PageId" description="Content blokken" link="" />
    <!--<tab id="SubPages" order="40" type="child" child="Pages" foreignkey="PageId" description="Subpagina's" link=""/>-->
    <tab id="Attachments" order="50" type="attachments" child="" foreignkey="" description="Bijlage" link="" />
  </tabs>
  <attachments>
    <attachment id="attachment1" show="1" required="0" tab="Attachments" width="1000" height="1000" type="image" description="Afbeelding" userdescription="0" help="" internal="0" />
  </attachments>
</object>
子体在XML文件中包含nodestag的节点。我尝试将XElement添加到子体和doc.Root中,但该元素没有保存到XML中。我怎样才能解决这个问题

编辑

通过添加到XElement来修复此问题


我认为您将其转换为ToList,这将创建另一个集合,并且您实际上正在向该集合添加新项目,而“文档”不会受到影响。在保存之前,请检查“doc”是否包含新添加的元素?如果您可以提供一个简短但完整的程序来演示问题,则会更容易为您提供帮助。目前,我们有很多代码都不太相关,但不足以真实再现问题。@MuhammadIdrees如果没有ToList,我如何添加新元素
foreach (DataRow row in dt.Rows)
{
    //check if the table name exists as the prefix of an object element in the instellingen.xml
    var query = from node in doc.Descendants("object")
                where node.Attribute("prefix").Value == row["TABLE_NAME"].ToString()
                select node.Element("nodes");



    //if the node is found
    if (query.Count() != 0)
    {
        var desecendants = query.Descendants("node").ToList();

        PropertyInfo[] properties = propDict[row["TABLE_NAME"].ToString()];


        foreach (var prop in properties)
        {
            int index = desecendants.FindIndex(item => item.Attribute("id").Value == prop.Name);
            if (index >= 0)
            {
                //Property is in list
                //TODO: check for property position changes 
            }
            else
            {
               doc.Root.Descendants("object").Where(objectnode => objectnode.Attribute("prefix").Value == row["TABLE_NAME"].ToString()).Elements("nodes").Descendants("node").ToList()
                /*desecendants*/.Add(new XElement("node",
                              new XAttribute("id", prop.Name),
                              new XAttribute("description", String.Empty),
                              new XAttribute("required", String.Empty),
                              new XAttribute("datatype", String.Empty), //could be auto generated
                              new XAttribute("nodetype", String.Empty),
                              new XAttribute("fieldlength", String.Empty),
                              new XAttribute("reference", String.Empty), 
                              new XAttribute("exclude", String.Empty),
                              new XAttribute("tab", String.Empty),
                              new XAttribute("order", String.Empty),
                              new XAttribute("default",String.Empty),
                              new XAttribute("decimals", String.Empty),
                              new XAttribute("maxchars",String.Empty),
                              new XAttribute("help", String.Empty),
                              new XAttribute("unique",String.Empty),
                              new XAttribute("show",String.Empty)));

            }
        }

    }
}

doc.Save(@"C:/instellingen.xml");
doc.Root.Descendants("object").Where(objectnode =>  objectnode.Attribute("prefix").Value == row["TABLE_NAME"].ToString()).Elements("nodes").LastOrDefault()