Click here to Skip to main content
15,888,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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:

XML
<?xml version="1.0" encoding="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

C#
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;
                    //MyTextBox1.AutoPostBack = true;
                    //MyTextBox1.EnableViewState = true;
                    //  MyTextBox1.Attributes.Add("runat", "server");
                   string name =  reader.Name.ToString();
                    MyTextBox1.Text = reader.Value.ToString();
                    Panel2.Controls.Add(MyTextBox1);


                

                    break;

                  



                case XmlNodeType.EndElement:

                  
                    if (treeDepth == 1)
                    {
                       
                    }

                    treeDepth--;

                    break;
            }

            count++;
        }

  
        //    sol1:

        //txtEditXML.Text = HttpContext.Current.Server.HtmlEncode(node.OuterXml);

        //txtEditXML.Text = node.InnerXml;

        //string strings = node.OuterXml;

        //string[] parts1 = strings.Split(new string[] { ">" }, StringSplitOptions.None);

        //// string[] split = strings.Split(new Char[] { '<', '>' },StringSplitOptions.RemoveEmptyEntries);

        //string[] split_innertext = strings.Split(new Char[] { '>', '<' },
        //                        StringSplitOptions.RemoveEmptyEntries);

        //Label mylabel = new Label();
        //mylabel.Visible = true;
        //mylabel.ID = "lbl" + count.ToString();

        //mylabel.Font.Bold = true;
        //// ViewState[mylabel.ID] = a.nodeName.ToString(); //adding view state 
        //mylabel.Text = node.Name + " " + ":";
        ////  ucSimpleControl.NodeName.Text = a.nodeName.ToString() + " " + ":";

        //Panel1.Controls.Add(mylabel);



        //TextBox MyTextBox1 = new TextBox();
        //MyTextBox1.CssClass = "textbox";
        //MyTextBox1.Visible = true;
        //MyTextBox1.ID = "tb" + count.ToString();

        //MyTextBox1.TextMode = TextBoxMode.MultiLine;
        ////MyTextBox1.AutoPostBack = true;
        ////MyTextBox1.EnableViewState = true;
        ////  MyTextBox1.Attributes.Add("runat", "server");
        //MyTextBox1.Text = node.InnerText.ToString();
        //Panel2.Controls.Add(MyTextBox1);





        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" ;  //txtEditXML.Text;
            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


C#
<?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>
Posted
Updated 23-Dec-15 3:35am
v4
Comments
Anisuzzaman Sumon 23-Dec-15 1:12am    
Show your XML file content
manoj1412012 23-Dec-15 6:16am    
Thanks Sumon as i have updated the question. XMl what i have is not well formed and I have very large XML file and for selected NOde I have to Display them IN the text Box and Lable and Update the XML So Any Help will be A great helpfor me Thanks For Support....
Anisuzzaman Sumon 23-Dec-15 7:56am    
Will you please show me the sample output you want/desire from the xml above?
manoj1412012 23-Dec-15 9:33am    
Sumon I have Updated the question and Thanks For your Support...

Lable text box

Author nair, nishantt
.................
.................
................
description A former architect battles



1 solution

C#
CmdString = "query";
        cmd = new SqlCommand(CmdString, con);
        sda = new SqlDataAdapter(cmd);
        dt = new DataTable();

        // Levelid,levelname  is retrieved in the DataTable 'dt'
        sda.Fill(dt);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Code = dt.Rows[i]["lId"].ToString();
            lName = dt.Rows[i]["Name"].ToString();

            // A TreeNode is created with current LevelNAme as text and Levelid as value
            LevelNode = new TreeNode(Name, Code);
TreeView1.Nodes.Add(LevelNode);

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900