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.
[quoted text, click to view] 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...