I have the following xml file:
<?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>
I am using LINQ to add another fragment tag to my xml file, which is:
XDocument xdocument = XDocument.Load("C:\\Users\\Administrator\\Desktop\\DM_Project.xml");
var id = "heading3";
var name="heading";
var content = "This is the data part";
var temp = xdocument.Descendants("fragment").First();
temp.Parent.Add(new XElement("fragment", content, new XAttribute("id", id), new XAttribute("name", name)));
var temp1 = xdocument.Descendants("fragment");
Console.WriteLine(temp1);
The result of the query is:
<fragment name="heading" id="heading1">
<h1>Heading 1</h1>
</fragment>
<fragment id="heading3" name="heading">This is the data part</fragment>
If one can observe a few fragment tag having id "heading3" is added. However, if the try to extract the fragment tags again, using the below query:
XDocument xdocument = XDocument.Load("C:\\Users\\Administrator\\Desktop\\DM_Project.xml");
var temp = xdocument.Descendants("fragment");
Console.WriteLine(temp);
The result is:
<fragment name="heading" id="heading1">
<h1>Heading 1</h1>
</fragment>
The newly added tag is missing. How can I get the data to be persistant? Am I missing something here? Or, this entire approach is itself wrong?
I am using LINQPad to test my queries.
Thank you.