we are loading the xml file path from a textbox as shown below
Session["CurrentXMLFile"] = txtFilePath.Text;
LoadXMLTree(txtFilePath.Text);
binding the xml file to tree view as shown below
private void LoadXMLTree(string fileName)
{
try
{
xmlDataSource.DataFile = fileName;
xmlDataSource.EnableCaching = false;
xmlTreeView.DataSourceID = "xmlDataSource";
xmlTreeView.DataBind();
}
This is the above code and i want to display only the parent nodes without the child node in the xml tree viewer.
The XMl what I have is not well formed XML and it is as following:
="1.0"="UTF-8"
<category>
<aaa>
<bbb>
<ccc>
<ddd>
<eee>
<abc>name</abc>
<cde>student</cde>
<fff>
<fgh>phone</fgh>
<hij>address</hij>
</fff>
</eee>
So the actual requirement is we have edit the xml when we select the specific node in the xml so i tried the following ways so i may not be correct so please
there is the code
protected void xmlTreeView_SelectedNodeChanged(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(Session["CurrentXMLFile"].ToString()));
XmlNode node = doc.SelectSingleNode(xmlTreeView.SelectedNode.DataPath);
XmlTextReader reader = new XmlTextReader(Server.MapPath(Session["CurrentXMLFile"].ToString()));
int treeDepth = 0;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Label mylabel = new Label();
mylabel.Visible = true;
mylabel.ID = "lbl" + count.ToString();
mylabel.Font.Bold = true;
mylabel.Text = reader.Name.ToString() + "" + ":" + System.Environment.NewLine;
Panel1.Controls.Add(mylabel);
if (treeDepth > 0)
{
treeDepth++;
}
break;
case XmlNodeType.Text:
TextBox MyTextBox1 = new TextBox();
MyTextBox1.Visible = true;
MyTextBox1.ID = "tb" + count.ToString();
MyTextBox1.TextMode = TextBoxMode.MultiLine;
string name = reader.Name.ToString();
MyTextBox1.Text = reader.Value.ToString();
Panel2.Controls.Add(MyTextBox1);
break;
case XmlNodeType.EndElement:
if (treeDepth == 1)
{
}
treeDepth--;
break;
}
count++;
}
Session["CurrentEditXpath"] = xmlTreeView.SelectedNode.DataPath;
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (Session["CurrentEditXpath"] != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(Session["CurrentXMLFile"].ToString()));
XmlNode node = doc.SelectSingleNode(Session["CurrentEditXpath"].ToString());
node.InnerXml = "test" ;
doc.Save(Server.MapPath(Session["CurrentXMLFile"].ToString()));
LoadXMLTree(Server.MapPath(Session["CurrentXMLFile"].ToString()));
lblMessage.Text = "XML file modified successfully!";
}
else
{
lblMessage.Text = "Please select a node to edit.";
}
}
}
in the above code i have tried using the spliter and string builders so the requriment as following
1)when user select specific parent node in the tree view we have populate all the child nodes content in the lable(outer xml) and text boxs(inner text).
so user can edit the xml so after editing the XMl he will click the save button.
2)save the changes made by the user in the save button.
sample XML Example as Following
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>nair, nishantt</author>
<title>Midnight Rain</title>
<genre>Fantasy4545454</genre>
<price>5.95</price>
<publish_date>2000-12-17</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk102">
<author>Ralls, eeee</author>
<title>Midnight werwer</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Evaffffffff</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-17</publish_date>
<description>This is a sample application to demonstrate the use of TreeView control to edit an XML file.
Author: Nishanth Nair (Nish@NishanthNair.com)</description>
</book>
</catalog>