Link to home
Create AccountLog in
Avatar of dsiemon
dsiemon

asked on

TreeView to XML (serialization) - Written in C# for an Intranet

OK, I have a very nice TreeView, nodes, and many levels deep
What i want to do is to save my TreeView to an XML File.. I am guessing this uses some type of Serialization

My undestanding is that there is more to Saving it, and then being able to Reload it, binding to the Treeview..
I think the routines to write the TreeView to XML are going to have to be write the XML a certain way, and then (MAYBE, if it is even needed)\produce an XSLT file too.. That way, if i want to Bind it to the Tree (XML to Treeview), i can just set the DataBinding and be done with it.. again, from what i understand..

Can someone help  build this? I could realy use the help

Thanks

 DSiemon
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.ComponentModel;
//using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Text;
 
namespace Hilti_Reorg
{
    public partial class _Default : System.Web.UI.Page
    {
        private SqlConnection sqlcon;
        protected void Page_Load(object sender, EventArgs e)
        {
 
                    
            DataSet dsNodeList = null;
            //showData();
            TreeView1.TreeNodeDataBound += bindNode;
 
            hilti(dsNodeList);
            //hilti_temp();
           
        }
        
  
 
        private void hilti(DataSet dsNodeList)
        {
            dsNodeList = ExecuteSP("GetTreeNodes2", null);
           // dsNodeList.WriteXml("C:\\OutputXML.xml"); 
            TreeView1.DataSource = new HierarchicalDataSet(dsNodeList, "ID", "ParentId");
           
            //HierarchicalDataSet ds = new HierarchicalDataSet(dsNodeList, "ID", "ParentId");
            //TreeView1.DataSource = ds;
            TreeView1.DataBind();
            //this.TreeView1.Attributes.Add("oncontextmenu", "RightClick();");
            //this.TreeView1.Attributes.Add("oncontextmenu", "TVRightClick();");
           //this.TreeView1.Attributes.Add("oncontextmenu", "TVRightClick();");
            TreeView1.Attributes.Add("onclick", "OnTreeClick(event);");
            //CallRecursive();
           //Declare Variable to geneate Random Number
           // SaveFileDialogsaveFile = new SaveFileDialog();
            //string strDir=""
           //string strFileName = "myTreeView.xml";
            //if (saveFile.ShowDialog() != DialogResult.OK) return;
 
            //TreeViewSerializer serializer = new TreeViewSerializer();
           // serializer.SerializeTreeView(TreeView1, strFileName);
 
 
  }
 
protected void bindNode(Object sender, TreeNodeEventArgs e)
{
    //TreeNode node = (TreeNode)e.Node ;
    //node.SelectAction.Attributes.Add("oncontextmenu", "TVRightClick();");
}
 
 
 
 protected void cmdCheckSelectNode1_Click (Object sender, System.EventArgs e) 
 {
//Checks If Parent Node has Child Node
 
if (TreeView1.CheckedNodes.Count > 0)
 {
 
//Display your selected nodes
 
 //For Each node = TreeNode In myTreeView.CheckedNodes
  foreach(System.Web.UI.WebControls.TreeNode node in TreeView1.CheckedNodes)
    {
     lblShowMessage.Text += node.Text + " " + node.Parent.Text + " ";
 
    }
 
 
 }
else
 {
     lblShowMessage.Text = "Sorry! You didnt select any node";
 }
 
 
 
 
}
 
 
        private void hilti_temp()
        {
            DataSet dataSet = new DataSet();
 
            dataSet.Tables.Add("Table");
            dataSet.Tables[0].Columns.Add("ID", typeof(int));
            dataSet.Tables[0].Columns.Add("ParentID", typeof(int));
            dataSet.Tables[0].Columns.Add("Text", typeof(string));
 
            DataRow row = dataSet.Tables[0].NewRow();
            row["ID"] = 1;
            row["Text"] = "Hilti";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 2;
            row["ParentID"] = 1;
            row["Text"] = "Corporate - Other";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 3;
            row["ParentID"] = 1;
            row["Text"] = "Hilti Australia";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 4;
            row["ParentID"] = 1;
            row["Text"] = "Hilti External Customers";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 5;
            row["ParentID"] = 4;
            row["Text"] = "A.P Savage";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 6;
            row["ParentID"] = 4;
            row["Text"] = "Appalachian Equipment";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Brock-White Company";
            row["ID"] = 7;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Brock-White Company Canada";
            row["ID"] = 8;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "CED Diamond Springs";
            row["ID"] = 9;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Contractor Supply Inc.";
            row["ID"] = 10;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Darragh Company";
            row["ID"] = 11;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Handyman Rentals";
            row["ID"] = 12;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Hartmann Building Specialty";
            row["ID"] = 13;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Heartland Industrial Supply Inc.";
            row["ID"] = 14;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
          
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Highland Lumber Company";
            row["ID"] = 15;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Hub Construction Supply";
            row["ID"] = 16;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "HUB Construction Supply - Sacramento";
            row["ID"] = 17;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
            
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Just Ask Rentals";
            row["ID"] = 18;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
         
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Lincoln Creek Lumber";
            row["ID"] = 19;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Mesquite Tool & Supply";
            row["ID"] = 20;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
          
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Pioneer Materials West Slope";
            row["ID"] = 21;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Pro Tool";
            row["ID"] = 22;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
         
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Ribco Supply";
            row["ID"] = 23;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
           
 
             row = dataSet.Tables[0].NewRow();
            row["Text"] = "Roethele Building Materials";
            row["ID"] = 24;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Stetson Building Products, Inc.";
            row["ID"] = 25;
            row["ParentID"] = 4;
            dataSet.Tables[0].Rows.Add(row);
           
 
             row = dataSet.Tables[0].NewRow();
            row["Text"] = "Hilti Latin America";
            row["ID"] = 26;
            row["ParentID"] = 1;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "ARGENTINA";
            row["ID"] = 27;
            row["ParentID"] = 26;
            dataSet.Tables[0].Rows.Add(row);
         
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Marketing Product Manager";
            row["ID"] = 28;
            row["ParentID"] = 27;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Field Engineer";
            row["ID"] = 29;
            row["ParentID"] = 28;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Field Engineer";
            row["ID"] = 30;
            row["ParentID"] = 28;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Marketing Product Assistant";
            row["ID"] = 31;
            row["ParentID"] = 28;
            dataSet.Tables[0].Rows.Add(row);
         
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Sales Director";
            row["ID"] = 32;
            row["ParentID"] = 28;
            dataSet.Tables[0].Rows.Add(row);
            
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "BC Supervisor";
            row["ID"] = 33;
            row["ParentID"] = 32;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "CS Supervisor";
            row["ID"] = 34;
            row["ParentID"] = 32;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "HC Supervisor";
            row["ID"] = 35;
            row["ParentID"] = 32;
            dataSet.Tables[0].Rows.Add(row);
         
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "I&G Supervisor";
            row["ID"] = 36;
            row["ParentID"] = 32;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Region Cuyo Supervisor";
            row["ID"] = 37;
            row["ParentID"] = 32;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "CARIBE";
            row["ID"] = 38;
            row["ParentID"] = 26;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Marketing Manager";
            row["ID"] = 39;
            row["ParentID"] = 38;
            dataSet.Tables[0].Rows.Add(row);
           
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Sales Manager";
            row["ID"] =40;
            row["ParentID"] = 38;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "CHILE";
            row["ID"] = 41;
            row["ParentID"] = 26;
            dataSet.Tables[0].Rows.Add(row);
          
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Fleet Management Manager";
            row["ID"] = 42;
            row["ParentID"] = 41;
            dataSet.Tables[0].Rows.Add(row);
         
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Sales Director - Center";
            row["ID"] = 42;
            row["ParentID"] = 41;
            dataSet.Tables[0].Rows.Add(row);
 
 
            //dataSet.WriteXml("C:\\OutputXML2.xml"); 
            TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");
            TreeView1.DataBind();
        }
 
        private void showData()
        {
             DataSet dataSet = new DataSet();
 
            dataSet.Tables.Add("Table");
            dataSet.Tables[0].Columns.Add("ID", typeof(int));
            dataSet.Tables[0].Columns.Add("ParentID", typeof(int));
            dataSet.Tables[0].Columns.Add("Text", typeof(string));
 
            DataRow row = dataSet.Tables[0].NewRow();
            row["ID"] = 1;
            row["Text"] = "Parent 1";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 2;
            row["Text"] = "Parent 2";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 3;
            row["ParentID"] = 1;
            row["Text"] = "Child 1";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 4;
            row["ParentID"] = 1;
            row["Text"] = "Child 2";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 5;
            row["ParentID"] = 2;
            row["Text"] = "Child 3";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["ID"] = 6;
            row["ParentID"] = 2;
            row["Text"] = "Child 4";
            dataSet.Tables[0].Rows.Add(row);
 
            row = dataSet.Tables[0].NewRow();
            row["Text"] = "Child 6";
            row["ID"] = 7;
            row["ParentID"] = 5;
            dataSet.Tables[0].Rows.Add(row);
 
          
 
            TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");
            TreeView1.DataBind();
           
        }
        
 
        private System.Data.DataSet ExecuteSP(string spname, string[][] parmArry)
        {
 
            System.Data.DataSet dsResult = new System.Data.DataSet();
 
            string strParameterNam;
 
            string strParameterVal;
 
            try
            {
 
                sqlcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
 
                if (sqlcon.State == System.Data.ConnectionState.Closed)
                {
 
                    sqlcon.Open();
 
                }
 
 
                SqlCommand cmd = sqlcon.CreateCommand();
 
                cmd.CommandText = spname;
 
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
 
 
                if (parmArry != null)
                {
 
                    for (int index = 0; index < parmArry.Length; index++)
                    {
 
                        strParameterNam = parmArry[index][0];
 
                        strParameterVal = parmArry[index][1];
 
                        if (strParameterNam != null && strParameterVal != null)
                        {
 
 
                        }
 
 
                    }
 
                }
 
                SqlDataAdapter da = new SqlDataAdapter(cmd);
 
                da.Fill(dsResult);
 
            }
 
            catch (Exception ex)
            {
 
                dsResult = null;
 
                Response.Write(ex.Message);
 
            }
 
            finally
            {
 
                sqlcon.Close();
 
 
            }
 
            return dsResult;
 
        }
 
 
    }
}
--------------------------------The ASPX Page-----------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Hilti_Reorg._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>xTreeView</title>
    <style type="text/css">
<!--
 
.skin0{
position:absolute;
width:140px;
border:2px solid black;
background-color:menu;
font-family:Verdana;
line-height:20px;
cursor:default;
font-size:14px;
z-index:100;
visibility:hidden;
}
 
.menuitems{
padding-left:10px;
padding-right:10px;
font-family:Verdana;
font-size:12px;
color:black;
}
-->
</style>
 
</head>
    
 
<body>
 <script type="text/javascript">
var menuskin = "skin0"; 
var display_url = 1; // Show URLs in status bar?
</script>
 
  <form id="theform" runat="server">
  <div id="ie5menu" class="skin0" onmouseover="highlightie5()" onmouseout="lowlightie5()"
            onclick="jumptoie5();">
            
        </div>
 
    <div>
 <asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" PathSeparator="|" ShowCheckBoxes="All">
 
 
 
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
 
 
 
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"      VerticalPadding="0px" />
 
 
 
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"      NodeSpacing="0px" VerticalPadding="0px" />
 
            <DataBindings>
                <asp:TreeNodeBinding DataMember="System.Data.DataRowView" 
                    TextField="Text" ValueField="ID" />
            </DataBindings>
 
</asp:TreeView>
 
 <asp:Panel ID="Panel2"  runat="server" style="display:none" BorderColor="Black" CssClass="skin0" onMouseover="highlightie5(event)" onMouseout="lowlightie5(event)" onClick="jumptoie5(event)"  >
                <div class="menuitems"><asp:LinkButton ID="LinkButton1" runat="server"   CssClass="menuitems" >New Node</asp:LinkButton></div>
                <div class="menuitems"><asp:LinkButton ID="LinkButton2" runat="server"   CssClass="menuitems">Edit Node</asp:LinkButton></div>
                <hr />
                <div class="menuitems"><asp:LinkButton ID="LinkButton3" runat="server"   CssClass="menuitems">Delete Node</asp:LinkButton></div>
                <hr />
                <div class="menuitems"><asp:LinkButton ID="LinkButton4" runat="server"   CssClass="menuitems">FAQS</asp:LinkButton></div>
                <div class="menuitems"><asp:LinkButton ID="LinkButton5" runat="server"   CssClass="menuitems">Online Help</asp:LinkButton></div>
                <hr />
                <div class="menuitems"><asp:LinkButton ID="LinkButton6" runat="server"   CssClass="menuitems">Email Me</asp:LinkButton></div>
            </asp:Panel>
            <br />
            <a href=""  > </a>
 
<asp:Button ID="cmdCheckSelectNode1" runat="server" Text="Nodes Selected" /><br /><br />
 
 
 
<asp:Label ID="lblShowMessage" runat="server" />  
 
  
    </div>
   </form>
<script src="xtreeview.js"  type="text/javascript"></script>
<script src="NodeStates.js"  type="text/javascript"></script>
<script src="CheckBoxes.js"  type="text/javascript"></script>
   <script type="text/javascript">
 
    // if (document.all) {
     //    ie5menu.className = menuskin;
    //     document.getElementById('TreeView1').oncontextmenu = showmenuie5;
     //    document.body.onclick = hidemenuie5;
    //  }
  var tree = document.getElementById("TreeView1");
  var tags = tree.getElementsByTagName("A");
  for(var i = 0; i < tags.length; i++){
    tags[i].oncontextmenu = test;//"alert('" + tags[i].id + "'); return false"; 
    tags[i].href = "javascript:void(null);";
    //if(i%300 == 0) alert(tags[i].outerHTML);     
  }
  function test()
  {
    //alert(event.srcElement.id);
    ie5menu.className = menuskin;
    showmenuie5();
 
 
    return false;
  }
 
    document.body.onclick = hidemenuie5; 
    </script>
    
   
</body>
</html>
-----------------------------------------------------------------------------

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marcus Keustermans
Marcus Keustermans
Flag of South Africa image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer