LINQ to XML新添加的XElement未保存到XML文件

时间:2015-09-07 09:50:31

标签: c# xml linq

我尝试更新XML文件,但新添加的XElement未保存到XML文件中。当我使用调试器时,我可以看到新XElement已添加到node's列表中,但在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>

相关代码

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");

descendants包含XML文件中node's标记的nodes。我尝试将XElement添加到descendants并添加到doc.Root,但该元素未保存到XML。我该如何解决这个问题?

编辑

通过添加到XElement

来修复它
doc.Root.Descendants("object").Where(objectnode =>  objectnode.Attribute("prefix").Value == row["TABLE_NAME"].ToString()).Elements("nodes").LastOrDefault()

0 个答案:

没有答案