如何在asp.net中的xml树视图控件中只显示父节点? [英] how to display only parent nodes in xml tree view control in asp.net?

查看:80
本文介绍了如何在asp.net中的xml树视图控件中只显示父节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.





我所拥有的XMl是不是格式良好的XML和它如下:





The XMl what I have is not well formed XML and it is as following:

<?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>





所以实际要求是我们在选择xml中的特定节点时编辑xml所以我尝试了以下方式,所以我可能不正确所以请



有代码





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


}


















$ b上面代码中的$ b我已经尝试过使用spliter和字符串构建器,所以请求如下:当用户在树视图中选择特定的父节点时,我们有以下内容:b

1)填充标签(外部xml)和文本框(内部文本)中的所有子节点内容。



因此用户可以编辑xml,因此在编辑XMl之后他将单击保存按钮。



2)保存用户在保存按钮中所做的更改。





示例XML示例如下














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>

推荐答案

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);

        }


这篇关于如何在asp.net中的xml树视图控件中只显示父节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆