Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用LINQ在xml中持久化新添加的标记_C#_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 无法使用LINQ在xml中持久化新添加的标记

C# 无法使用LINQ在xml中持久化新添加的标记,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我有以下xml文件: <?xml version="1.0" encoding="UTF-8"?> <!-- New XML document created with EditiX XML Editor (http://www.editix.com) at Tue Mar 18 22:41:05 IST 2014 --> <html xsi:NamespaceSchemaLocation="http://www.w3.org/1999/xhtml DM_Pro

我有以下xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<!-- New XML document created with EditiX XML Editor (http://www.editix.com) at Tue Mar 18 22:41:05 IST 2014 
-->
<html xsi:NamespaceSchemaLocation="http://www.w3.org/1999/xhtml DM_Project.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <head>
        <title>title1</title>
    </head>
    <body>
        <fragment name="heading" id="heading1">
            <h1>Heading 1</h1>
        </fragment>
    </body>
</html>
查询结果为:

<fragment name="heading" id="heading1">
  <h1>Heading 1</h1>
</fragment> 
<fragment id="heading3" name="heading">This is the data part</fragment> 
结果是:

 <fragment name="heading" id="heading1">
      <h1>Heading 1</h1>
    </fragment>

标题1
缺少新添加的标记。如何使数据持久化?我是不是遗漏了什么?或者,整个方法本身就是错误的

我正在使用LINQPad测试我的查询


谢谢。

您没有将文档保存回文件。完成编辑后,请调用:

下次当您执行代码时(即加载文档并查询它),将有新元素

XDocument xdocument = XDocument.Load("C:\\Users\\Administrator\\Desktop\\DM_Project.xml");
var temp = xdocument.Descendants("fragment");
Console.WriteLine(temp);
 <fragment name="heading" id="heading1">
      <h1>Heading 1</h1>
    </fragment>
string path = @"C:\Users\Administrator\Desktop\DM_Project.xml";
XDocument xdocument = XDocument.Load(path);
// modify document
xdocument.Save(path);