473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

inserting record into an xml doc using datagrid

I have the following for a datagrid that inserts a row into an xml
file. It works great, but inserts the data to a new top-level node. I
don't know how to specifiy an existing node for the inserted item:

void DataGrid1_Update(Object Sender, DataGridCommandEventArgs e) {
try {
string stitle = (e.Item.Cells[1].FindControl("txttitle") as
TextBox).Text;
DataSet ds = new DataSet();

//ds.ReadXmlSchema(Server.MapPath("~/Home/XMLMenus/MenuCategory.xsd"));

ds.ReadXml(Server.MapPath("~/web1.sitemap"),XmlReadMode.InferSchema);
DataRow dr = ds.Tables[0].Rows[e.Item.ItemIndex];
dr["title"] = stitle;
dr.AcceptChanges();
ds.WriteXml(Server.MapPath("~/web1.sitemap"));
DataGrid1.EditItemIndex = -1;
BindCategoryList();
}

Actually, this code is just overwriting the xml doc and probably won't
work for what I need, so I am looking for ideas...

Nov 12 '05 #1
1 1326
Here is what I came up with. I have a treeview control that loads the
sitemap. I have three textboxes for the selected treenode, url, and
title values. then the following code inserts a new node using the
values from the textboxes:

void lnAddNode_Click(Object sender, EventArgs e) {
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/web1.sitemap"));

string xmlQuery = "//siteMapNode[@title='" + txtNode.Text +
"']";
XmlNode xmlInsertNode = doc.SelectSingleNode(xmlQuery);

XmlNode root = doc.DocumentElement;

// Add a price element.
XmlElement elem = doc.CreateElement("siteMapNode");
elem.SetAttribute("url", txtURL.Text);
elem.SetAttribute("title", txtTitle.Text);
elem.SetAttribute("description", "");
elem.SetAttribute("roles", "");

//Add the node to the document.
xmlInsertNode.InsertAfter(elem, xmlInsertNode.LastChild);

// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new
XmlTextWriter(Server.MapPath("~/web1.sitemap"),null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
}

The only issue I run into is a locked file when refreshing. I am still
researching this one.

Stephen wrote:
I have the following for a datagrid that inserts a row into an xml
file. It works great, but inserts the data to a new top-level node. I
don't know how to specifiy an existing node for the inserted item:

void DataGrid1_Update(Object Sender, DataGridCommandEventArgs e) {
try {
string stitle = (e.Item.Cells[1].FindControl("txttitle") as
TextBox).Text;
DataSet ds = new DataSet();

//ds.ReadXmlSchema(Server.MapPath("~/Home/XMLMenus/MenuCategory.xsd"));

ds.ReadXml(Server.MapPath("~/web1.sitemap"),XmlReadMode.InferSchema);
DataRow dr = ds.Tables[0].Rows[e.Item.ItemIndex];
dr["title"] = stitle;
dr.AcceptChanges();
ds.WriteXml(Server.MapPath("~/web1.sitemap"));
DataGrid1.EditItemIndex = -1;
BindCategoryList();
}

Actually, this code is just overwriting the xml doc and probably won't
work for what I need, so I am looking for ideas...


Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mr Marsh | last post by:
Hello, I'll try and provide as much information as I can about the problem/challenge I'm facing. I have found similar postings to my enquiry but they don't really hit the nail on the head. I am...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
0
by: Paolo Tardivel via DotNetMonster.com | last post by:
I have a databound datagrid containing template columns which allow editing of the values contained in the datagrid through the use of text boxes. One of the fields in the datagrid displays the...
7
by: GaryB | last post by:
I have an untyped datatable that has financial numbers and controls that were populated by code (not a simple fill from a DA). Now I want to insert subtotals into it. I wrote a sub to do so that...
6
by: HS1 | last post by:
Hello I have a table in Access Database. This table has a AutoNumber field. I use a DataGrid to show that table When I insert a new record in for this table using a DataGrid, there is a...
0
by: Michael Turner | last post by:
Hi Guys Does anyone have an example of allowing a data to insert data when a new row is inserted or updating if there has just been a change in a datagrid, I have a problem where all the...
3
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be...
8
by: =?Utf-8?B?bWlrZWc=?= | last post by:
Hi, I am building a small Help Desk application for my company and need to be able to edit "open" help desk issues. I use a simple datagrid to display each issue (6 per page) , with an Edit...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.