XML inserting new record conflictionhttp://forums.asp.net/t/1776550.aspx/1?XML+inserting+new+record+conflictionMon, 05 Mar 2012 04:08:05 -050017765504863078http://forums.asp.net/p/1776550/4863078.aspx/1?XML+inserting+new+record+conflictionXML inserting new record confliction <p><strong>Hello this is the aspx page</strong></p> <p>&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Default6.aspx.cs&quot; Inherits=&quot;Default6&quot; %&gt;</p> <p>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</p> <p>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br> &lt;head runat=&quot;server&quot;&gt;<br> &lt;title&gt;&lt;/title&gt;<br> &lt;/head&gt;<br> &lt;body&gt;<br> &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<br> &lt;asp:Repeater ID=&quot;Repeater1&quot; runat=&quot;server&quot; DataSourceID=&quot;XmlDataSource1&quot;&gt;<br> &lt;ItemTemplate&gt;<br> Painter:<br> &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text='&lt;%# XPath(&quot;@country&quot;) %&gt;' /&gt;<br> &lt;br /&gt;<br> &amp;nbsp; Nationality:<br> &lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text='&lt;%# XPath(&quot;title&quot;) %&gt;' /&gt;<br> &lt;br /&gt;<br> &lt;asp:Repeater ID=&quot;Repeater2&quot; runat=&quot;server&quot; DataSource='&lt;%# XPathSelect(&quot;Paintings/Title&quot;) %&gt;'&gt;<br> &lt;ItemTemplate&gt;<br> &amp;nbsp; &amp;nbsp; Name of painting: &lt;asp:Label ID=&quot;Label2&quot; runat=&quot;server&quot; Text='&lt;%# XPath(&quot;@name&quot;) %&gt;' /&gt;<br> &lt;br /&gt;<br> &amp;nbsp; &amp;nbsp; Year of painting: &lt;asp:Label ID=&quot;Label4&quot; runat=&quot;server&quot; Text='&lt;%# XPath(&quot;Year&quot;) %&gt;' /&gt;<br> &lt;/ItemTemplate&gt;<br> &lt;SeparatorTemplate&gt;<br> &lt;br /&gt;<br> &lt;/SeparatorTemplate&gt;<br> &lt;/asp:Repeater&gt;<br> &lt;/ItemTemplate&gt;<br> &lt;SeparatorTemplate&gt;<br> &lt;br /&gt;<br> &lt;/SeparatorTemplate&gt;<br> &lt;/asp:Repeater&gt;<br> &lt;asp:XmlDataSource ID=&quot;XmlDataSource1&quot; runat=&quot;server&quot; DataFile=&quot;~/Data.xml&quot;&gt;&lt;/asp:XmlDataSource&gt;<br> &lt;br /&gt;<br> &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;<br> &lt;asp:TextBox ID=&quot;TextBox2&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;<br> &lt;asp:TextBox ID=&quot;TextBox3&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;<br> &lt;asp:TextBox ID=&quot;TextBox4&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;<br> &lt;br /&gt;<br> &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;add&quot; onclick=&quot;Button1_Click&quot; /&gt;<br> &lt;/form&gt;<br> &lt;/body&gt;<br> &lt;/html&gt;</p> <p><strong>&nbsp;this is the code behind</strong><br> <br> &nbsp;</p> <p>using System;<br> using System.Collections.Generic;<br> using System.Linq;<br> using System.Web;<br> using System.Web.UI;<br> using System.Web.UI.WebControls;<br> using System.Data;<br> using System.Xml;<br> using System.Xml.XPath;</p> <p>public partial class Default6 : System.Web.UI.Page<br> {<br> protected void Page_Load(object sender, EventArgs e)<br> {<br> if (!Page.IsPostBack)<br> {<br> LoadXmlData();<br> }<br> }</p> <p>private void LoadXmlData()<br> {<br> DataSet ds = new DataSet();<br> ds.ReadXml(Server.MapPath(&quot;Data.xml&quot;));<br> }<br> protected void Button1_Click(object sender, EventArgs e)<br> {<br> try<br> {<br> XmlTextReader reader = new XmlTextReader(Server.MapPath(&quot;Data.xml&quot;));<br> XmlDocument doc = new XmlDocument();<br> doc.Load(reader);<br> reader.Close();<br> XmlNode currNode;<br> XmlDocumentFragment docfrag = doc.CreateDocumentFragment();<br> docfrag.InnerText = &quot;&lt;cd country=\&quot;&quot; &#43; TextBox1.Text &#43; &quot;\&quot;&gt;&quot; &#43; <br> &quot;&lt;title&gt;&quot; &#43; TextBox2.Text &#43; &quot;&lt;/title&gt;&quot; &#43; <br> &quot;&lt;artist&gt;&quot; &#43; TextBox3.Text &#43; &quot;&lt;/artist&gt;&quot; &#43;<br> &quot;&lt;price&gt;&quot; &#43; TextBox4.Text &#43; &quot;&lt;/price&gt;&quot; &#43;<br> &quot;&lt;/cd&gt;&quot;;<br> currNode = doc.DocumentElement;<br> currNode.InsertAfter(docfrag, currNode.LastChild);<br> doc.Save(Server.MapPath(&quot;Data.xml&quot;));</p> <p>}<br> catch(Exception ex)<br> {<br> <br> Console.WriteLine(&quot;Exception: {0}&quot;, ex.ToString());<br> }</p> <p><br> LoadXmlData();<br> }<br> }</p> <p><strong>This is my XML Data before inserting</strong></p> <p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br> &lt;catalog&gt;<br> &lt;cd country=&quot;USA&quot;&gt;<br> &lt;title&gt;Empire Burlesque&lt;/title&gt;<br> &lt;artist&gt;Bob Dylan&lt;/artist&gt;<br> &lt;Paintings&gt;<br> &lt;Title name=&quot;sdfsd&quot;&gt;<br> &lt;/Title&gt;<br> &lt;/Paintings&gt;<br> &lt;price&gt;10.90&lt;/price&gt;<br> &lt;/cd&gt;<br> &lt;cd country=&quot;UK&quot;&gt;<br> &lt;title&gt;Hide your heart&lt;/title&gt;<br> &lt;artist&gt;Bonnie Tyler&lt;/artist&gt;<br> &lt;price&gt;10.0&lt;/price&gt;<br> &lt;/cd&gt;<br> &lt;cd country=&quot;USA&quot;&gt;<br> &lt;title&gt;Greatest Hits&lt;/title&gt;<br> &lt;artist&gt;Dolly Parton&lt;/artist&gt;<br> &lt;price&gt;9.90&lt;/price&gt;<br> &lt;/cd&gt;<br> &lt;/catalog&gt;</p> <p><strong>This is the XML data after inserting where i am having the problem</strong></p> <p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br> &lt;catalog&gt;<br> &lt;cd country=&quot;USA&quot;&gt;<br> &lt;title&gt;Empire Burlesque&lt;/title&gt;<br> &lt;artist&gt;Bob Dylan&lt;/artist&gt;<br> &lt;Paintings&gt;<br> &lt;Title name=&quot;sdfsd&quot;&gt;<br> &lt;/Title&gt;<br> &lt;/Paintings&gt;<br> &lt;price&gt;10.90&lt;/price&gt;<br> &lt;/cd&gt;<br> &lt;cd country=&quot;UK&quot;&gt;<br> &lt;title&gt;Hide your heart&lt;/title&gt;<br> &lt;artist&gt;Bonnie Tyler&lt;/artist&gt;<br> &lt;price&gt;10.0&lt;/price&gt;<br> &lt;/cd&gt;<br> &lt;cd country=&quot;USA&quot;&gt;<br> &lt;title&gt;Greatest Hits&lt;/title&gt;<br> &lt;artist&gt;Dolly Parton&lt;/artist&gt;<br> &lt;price&gt;9.90&lt;/price&gt;<br> &lt;/cd&gt;<strong>&amp;lt;cd country=&quot;USA &quot;&amp;gt;&amp;lt;title&amp;gt;Hide your heart &amp;lt;/title&amp;gt;&amp;lt;artist&amp;gt;something&amp;lt;/artist&amp;gt;&amp;lt;price&amp;gt;something&amp;lt;/price&amp;gt;&amp;lt;/cd&amp;gt;&lt;/catalog&gt;</strong></p> <p><strong>I need simply to add data to<br> &nbsp;&lt;cd country=&quot;USA&quot;&gt; to make new record as example&nbsp;&lt;cd country=&quot;UK&quot;&gt;<br> </strong><strong>&lt;title&gt;Empire Burlesque&lt;/title&gt;&nbsp;<strong>&nbsp;to make new record as example &nbsp;<strong>&lt;title&gt;Something&lt;/title&gt;<br> &lt;Paintings&gt;<br> &lt;Title name=&quot;sdfsd&quot;&gt;&nbsp;<strong><strong>to make new record as example&nbsp;<strong><strong><strong>&lt;Paintings&gt;<br> &lt;Title name=&quot;Something&quot;&gt;</strong></strong></strong></strong></strong><br> </strong></strong></strong></p> <p><strong>any help Thanks</strong></p> 2012-03-04T20:08:18-05:004863314http://forums.asp.net/p/1776550/4863314.aspx/1?Re+XML+inserting+new+record+conflictionRe: XML inserting new record confliction <p>Hi,</p> <p>&nbsp;&nbsp; Use InneXML Property insteda of InnerText. Here is an Example :</p> <p>&nbsp;</p> <pre class="prettyprint">public class Sample { public static void Main() { // Create the XmlDocument. XmlDocument doc = new XmlDocument(); // Create a document fragment. XmlDocumentFragment docFrag = doc.CreateDocumentFragment(); // Set the contents of the document fragment. docFrag.InnerXml =&quot;&lt;item&gt;widget&lt;/item&gt;&quot;; // Display the document fragment. Console.WriteLine(docFrag.InnerXml); } }</pre> 2012-03-05T03:53:48-05:004863336http://forums.asp.net/p/1776550/4863336.aspx/1?Re+XML+inserting+new+record+conflictionRe: XML inserting new record confliction <p>Hi,</p> <p>&nbsp;&nbsp; here is the modified code. Check it out and let us know the result.</p> <pre class="prettyprint">protected void Button1_Click(object sender, EventArgs e) { try { XmlTextReader reader = new XmlTextReader(Server.MapPath(&quot;~/XML/XMLFile3.xml&quot;)); XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlNode currNode; XmlDocumentFragment docfrag = doc.CreateDocumentFragment(); docfrag.InnerXml = &quot;&lt;cd country=\&quot;&quot; &#43; TextBox1.Text &#43; &quot;\&quot;&gt;&quot; &#43; &quot;&lt;title&gt;&quot; &#43; TextBox2.Text &#43; &quot;&lt;/title&gt;&quot; &#43; &quot;&lt;artist&gt;&quot; &#43; TextBox3.Text &#43; &quot;&lt;/artist&gt;&quot; &#43; &quot;&lt;price&gt;&quot; &#43; TextBox4.Text &#43; &quot;&lt;/price&gt;&quot; &#43; &quot;&lt;/cd&gt;&quot;; currNode = doc.DocumentElement; currNode.InsertAfter(docfrag, currNode.LastChild); doc.Save(Server.MapPath(&quot;~/XML/XMLFile3.xml&quot;)); } catch (Exception ex) { Console.WriteLine(&quot;Exception: {0}&quot;, ex.ToString()); } LoadXmlData(); }</pre> 2012-03-05T04:08:05-05:00