using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Xml.XPath;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadXmlData();
}
}
I need simply to add data to
<cd country="USA"> to make new record as example <cd country="UK"> <title>Empire Burlesque</title> to make new record as example <title>Something</title>
<Paintings>
<Title name="sdfsd"> to make new record as example <Paintings>
<Title name="Something">
Use InneXML Property insteda of InnerText. Here is an Example :
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 ="<item>widget</item>";
// Display the document fragment.
Console.WriteLine(docFrag.InnerXml);
}
}
Please "Mark As Answer;", if this Post helps you.
Visit My Blog
khayalian
Member
127 Points
143 Posts
XML inserting new record confliction
Mar 04, 2012 08:08 PM|LINK
Hello this is the aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
<ItemTemplate>
Painter:
<asp:Label ID="Label1" runat="server" Text='<%# XPath("@country") %>' />
<br />
Nationality:
<asp:Label ID="Label3" runat="server" Text='<%# XPath("title") %>' />
<br />
<asp:Repeater ID="Repeater2" runat="server" DataSource='<%# XPathSelect("Paintings/Title") %>'>
<ItemTemplate>
Name of painting: <asp:Label ID="Label2" runat="server" Text='<%# XPath("@name") %>' />
<br />
Year of painting: <asp:Label ID="Label4" runat="server" Text='<%# XPath("Year") %>' />
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Data.xml"></asp:XmlDataSource>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="add" onclick="Button1_Click" />
</form>
</body>
</html>
this is the code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Xml.XPath;
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadXmlData();
}
}
private void LoadXmlData()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Data.xml"));
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
XmlTextReader reader = new XmlTextReader(Server.MapPath("Data.xml"));
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
XmlNode currNode;
XmlDocumentFragment docfrag = doc.CreateDocumentFragment();
docfrag.InnerText = "<cd country=\"" + TextBox1.Text + "\">" +
"<title>" + TextBox2.Text + "</title>" +
"<artist>" + TextBox3.Text + "</artist>" +
"<price>" + TextBox4.Text + "</price>" +
"</cd>";
currNode = doc.DocumentElement;
currNode.InsertAfter(docfrag, currNode.LastChild);
doc.Save(Server.MapPath("Data.xml"));
}
catch(Exception ex)
{
Console.WriteLine("Exception: {0}", ex.ToString());
}
LoadXmlData();
}
}
This is my XML Data before inserting
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<Paintings>
<Title name="sdfsd">
</Title>
</Paintings>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>10.0</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
</catalog>
This is the XML data after inserting where i am having the problem
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<Paintings>
<Title name="sdfsd">
</Title>
</Paintings>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>10.0</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd><cd country="USA "><title>Hide your heart </title><artist>something</artist><price>something</price></cd></catalog>
I need simply to add data to
<cd country="USA"> to make new record as example <cd country="UK">
<title>Empire Burlesque</title> to make new record as example <title>Something</title>
<Paintings>
<Title name="sdfsd"> to make new record as example <Paintings>
<Title name="Something">
any help Thanks
kuber.manral
Contributor
3051 Points
714 Posts
Re: XML inserting new record confliction
Mar 05, 2012 03:53 AM|LINK
Hi,
Use InneXML Property insteda of InnerText. Here is an Example :
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 ="<item>widget</item>"; // Display the document fragment. Console.WriteLine(docFrag.InnerXml); } }Visit My Blog
kuber.manral
Contributor
3051 Points
714 Posts
Re: XML inserting new record confliction
Mar 05, 2012 04:08 AM|LINK
Hi,
here is the modified code. Check it out and let us know the result.
protected void Button1_Click(object sender, EventArgs e) { try { XmlTextReader reader = new XmlTextReader(Server.MapPath("~/XML/XMLFile3.xml")); XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlNode currNode; XmlDocumentFragment docfrag = doc.CreateDocumentFragment(); docfrag.InnerXml = "<cd country=\"" + TextBox1.Text + "\">" + "<title>" + TextBox2.Text + "</title>" + "<artist>" + TextBox3.Text + "</artist>" + "<price>" + TextBox4.Text + "</price>" + "</cd>"; currNode = doc.DocumentElement; currNode.InsertAfter(docfrag, currNode.LastChild); doc.Save(Server.MapPath("~/XML/XMLFile3.xml")); } catch (Exception ex) { Console.WriteLine("Exception: {0}", ex.ToString()); } LoadXmlData(); }Visit My Blog