Tree View of XML file: Static|Dyanmic

by nazmul2. July 2008 01:19

First Static, Here is the design file and no codebehind

<asp:XmlDataSource ID="dsXML" runat=server DataFile="cd_catalog.xml"

XPath="/CATALOG/CD" ></asp:XmlDataSource>

<asp:TreeView runat="server" ID="xmlTree" DataSourceID="dsXML">

<DataBindings>

<asp:TreeNodeBinding DataMember="TITLE" TextField="#InnerText" />

<asp:TreeNodeBinding DataMember="ARTIST" TextField="#InnerText" />

<asp:TreeNodeBinding DataMember="COUNTRY" TextField="#InnerText" />

</DataBindings>

</asp:TreeView>

and few lines of xml file 

<?xml version="1.0" encoding="ISO-8859-1"?>

<CATALOG>

<CD>

<TITLE>Empire Burlesque</TITLE>

<ARTIST>Bob Dylan</ARTIST>

<COUNTRY>USA</COUNTRY>

<COMPANY>Columbia</COMPANY>

<PRICE>10.90</PRICE>

<YEAR>1985</YEAR>

</CD>

<CD>

Now Dynamic,the dynamic one is little bit tricky,

<h3>TreeView Constructor Example</h3>
<asp:PlaceHolder id="ControlPlaceHolder" runat="server">
</asp:PlaceHolder>
<asp:XmlDataSource id="BookXmlDataSource" DataFile="Book.xml" runat="server">
</asp:XmlDataSource>
<br /><br />
<asp:Label id="Message" runat="server"/>

using System;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
public partial class XmlMgt_XmlReader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){
// Create a new TreeView control.
TreeView NewTree = new TreeView();
// Set the properties of the TreeView control.
NewTree.ID = "BookTreeView";
NewTree.DataSourceID ="BookXmlDataSource";
// Create the tree node binding relationship.
// Create the root node binding.
TreeNodeBinding RootBinding = new TreeNodeBinding();
RootBinding.DataMember = "Book";
RootBinding.TextField = "Title";
// Create the parent node binding.
TreeNodeBinding ParentBinding = new TreeNodeBinding();
ParentBinding.DataMember = "Chapter";
ParentBinding.TextField = "Heading";
// Create the leaf node binding.
TreeNodeBinding LeafBinding = new TreeNodeBinding();
LeafBinding.DataMember = "Section";
LeafBinding.TextField = "Heading";
// Add bindings to the DataBindings collection.
NewTree.DataBindings.Add(RootBinding);
NewTree.DataBindings.Add(ParentBinding);
NewTree.DataBindings.Add(LeafBinding);
// Manually register the event handler for the SelectedNodeChanged event.
NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);
// Add the TreeView control to the Controls collection of thePlaceHolder control.
ControlPlaceHolder.Controls.Add(NewTree);
}
protected void Node_Change(object sender, EventArgs e){
// Retrieve the TreeView control from the Controls collectionof the PlaceHolder control.
TreeView LocalTree =(TreeView)ControlPlaceHolder.FindControl("BookTreeView");
// Display the selected node.
Message.Text = "You selected: " + LocalTree.SelectedNode.Text;
}
}
<?xml version="1.0" encoding="utf-8" ?>

<Books>

<Book Title="Communication Skill">

<Chapter Heading="Chapter 1">

<Section Heading="Section 1">

</Section>

<Section Heading="Section 2">

</Section>

</Chapter>

<Chapter Heading="Chapter 2">

<Section Heading="Section 1">

</Section>

</Chapter>

</Book>

</Books>

This is shorter than earlier with code-behind apporach

.aspx.cs file

protected void Page_Load(Object sender, EventArgs e)
{
TreeView NewTree = new TreeView();
// Set the properties of the TreeView control.
NewTree.ID = "BookTreeView";
NewTree.DataSourceID ="BookXmlDataSource";
// Manually register the event handler for the SelectedNodeChanged event.
NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);
// Add the TreeView control to the Controls collection of the PlaceHolder control.
ControlPlaceHolder.Controls.Add(NewTree);
}
protected void Node_Change(Object sender, EventArgs e)
{
// Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");
// Display the selected node.
Message.Text = "You selected: " + LocalTree.SelectedNode.Text;
}

Here is the design file: .aspx file

<asp:PlaceHolder id="ControlPlaceHolder" runat="server">
</asp:PlaceHolder>
<asp:XmlDataSource id="BookXmlDataSource" DataFile="cd_catalog.xml"
runat="server">
</asp:XmlDataSource>

More shorter of the TreeView, absolutely in codebehind

.aspx.cs file

protected void Page_Load(Object sender, EventArgs e)
{
//creating an instance of XmlDatasource
XmlDataSource ds = new XmlDataSource();
//connecting the source with file
String _path = Server.MapPath("cd_catalog.xml");
ds.DataFile = _path;
//binding the file
ds.DataBind();
//creating an instnace of TreeView
TreeView NewTree = new TreeView();
//Telling its source
NewTree.DataSource = ds;
//binding the TreeView with DataSource
NewTree.DataBind();
//creating a PlaceHolder
PlaceHolder plcHolder = new PlaceHolder();
//adding the PlaceHolder with FORM
form1.Controls.Add(plcHolder);
//adding the TreeView Control to the PlaceHolder
plcHolder.Controls.Add(NewTree);
}

Tags:

ASP.net | C Sharp | C# | XML

How to read XML file

by nazmul2. July 2008 00:57
using System;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
public partial class XmlMgt_XmlReader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//creating an instance of XmlDocument
XmlDocument xmldoc = new XmlDocument();
//fill the document with an .xml file
String _path = Server.MapPath("cd_catalog.xml");
xmldoc.Load(_path);
//find the list of nodes 
//<Catalog>
//<CD>
//   <Title>..
//   <Artist>..
//   .....
//</CD>
//....
//</Catalog>
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("CD");
Response.Write("no. of Nodes :" + xmlnode.Count + "<br/>");
for (int i = 0; i < xmlnode.Count; i++)
{
//XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;
////XML Attribute Name and Value returned
////Example: <Book id = "001">
//Console.Write(xmlattrc[0].Name);
//Console.WriteLine(":\t"+xmlattrc[0].Value);
//First Child of the XML file - Catalog.xml - returned
//Example: <Author>Mark</Author>
//Response.Write(xmlnode[i].FirstChild.Name+"<br/>");
//Response.Write(xmlnode[i].FirstChild.InnerText + "<br/>");
////Last Child of the XML file - Catalog.xml - returned
////Example: <Publisher>Sams</Publisher>
//Response.Write(xmlnode[i].LastChild.Name + "<br/>");
//Response.Write(xmlnode[i].LastChild.InnerText+"<br/>");
if (xmlnode[i].HasChildNodes)
{
foreach (XmlElement chiElement in xmlnode[i])
Response.Write(chiElement.Name + ":" + chiElement.InnerText + "<br/>");
}
else
{
Response.Write("this is not childNode=" + i);
}
}
}
}

Tags: , ,

ASP.net | C Sharp | C# | XML

Design a class file for error trapping in ASP.net

by nazmul2. July 2008 00:14

myLog.cs File 

using System;
using System.IO;
using System.Text;
public class myLog
{
private string sLogFormat;
private string sErrorTime;
public myLog()
{
sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " +
DateTime.Now.ToLongTimeString().ToString() + " ==> ";
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear + sMonth + sDay;
}
public void ErrorLog(string sPathName, string sMsg, int iMsg)
{
StreamWriter sw = new StreamWriter(sPathName + "/" + sErrorTime + ".txt", true);
if (sMsg.Length > 0)
sw.WriteLine(sLogFormat + sMsg);
if (iMsg.ToString().Length > 0)
sw.WriteLine(sLogFormat + iMsg);
sw.Flush();
sw.Close();
}
}

Usage in .aspx.cs file 

myLog daylog = new myLog();
string _path = Server.MapPath(".");
daylog.ErrorLog(_path,"this is string",777);

Tags:

ASP.net | C Sharp | C#

About the author

You can find me in Stackoverflow.com where I asked  and answered questions.

  profile for Hoque on Stack Exchange, a network of free, community-driven Q&A sites

You can also find me in asp.net forum where I made posts more than 800.

Month List