C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » XML »

XML nodes in tree view


Posted Date: 14 Nov 2008    Resource Type: Code Snippets    Category: XML
Author: Deepika HaridasMember Level: Diamond    
Rating: 1 out of 5Points: 10



This is sample which will select a XML from Dialog Box and display all its nodes in the tree view form. Almost parsing of an XML.


private void menuItem3_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog(this);
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
// Initialize Buttons
EnableDisableControls();

// Initialize All the Arrays
treeView1.Nodes.Clear();
//listBox1.Items.Clear() ;
TreeNodeArray.Clear();

bFileLoaded = false;

XMLInputFile = openFileDialog1.FileName;
this.Text = OrigFormTitle + " ..." + XMLInputFile;
openFileDialog1.Dispose();

// Get the filename and filesize
FileInfo f = new FileInfo(XMLInputFile);
FileSize = f.Length.ToString();

// Begin thread to read input file and load into the ListBox
Thread t = new Thread(new ThreadStart(PopulateList));
t.Start();

// Begin thread to read input file and populate the Tree
Thread tt = new Thread(new ThreadStart(PopulateTree));
tt.Start();
}
private void PopulateList()
{
// Load the File
LoadFileIntoListBox();
}
private void PopulateTree()
{
// TreeView Nodes cannot be added in a thread , until the thread is marshalled
// using an Invoke or beginInvoke call.
// We create a delegate ( Funtion Pointer ) and invoke the thread using he delegate
MyDelegate dlg_obj;
dlg_obj = new MyDelegate(ParseFile);
treeView1.Invoke(dlg_obj);
}
private void ParseFile()
{
// Use the XMLReader class to read the XML File and populate the
// treeview
try
{
XmlTextReader reader = null;
reader = new XmlTextReader(XMLInputFile);
reader.WhitespaceHandling = WhitespaceHandling.None;
string readerName = "";
bool start_node = false;
int depth = 0;
TreeNode WORKINGNODE = null;
RootNode = null;
TreeNode AttrNode = null;
TreeNode newNode = null;
bool bIsEmpty = false;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
readerName = reader.Name;
bIsEmpty = reader.IsEmptyElement;

if (!start_node)
{
start_node = true;
RootNode = this.treeView1.Nodes.Add(readerName);
AssociateTag(RootNode, reader.LineNumber);
RootNode.SelectedImageIndex = 0;
RootNode.ImageIndex = 0;
continue;
}
depth = reader.Depth;

if (reader.IsStartElement() && depth == 1)
{
WORKINGNODE = RootNode.Nodes.Add(reader.Name);
AssociateTag(WORKINGNODE, reader.LineNumber);
}
else
{
TreeNode parent = WORKINGNODE;
WORKINGNODE = parent.Nodes.Add(reader.Name);
AssociateTag(WORKINGNODE, reader.LineNumber);
}

WORKINGNODE.SelectedImageIndex = 1;
WORKINGNODE.ImageIndex = 1;

for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
string rValue = reader.Value.Replace("\r\n", " ");
AttrNode = WORKINGNODE.Nodes.Add(reader.Name);
// AttrNode = WORKINGNODE.Nodes.Add(reader.Name +"="+rValue);
AssociateTag(AttrNode, reader.LineNumber);

AttrNode.SelectedImageIndex = 1;
AttrNode.ImageIndex = 1;
TreeNode tmp = AttrNode.Nodes.Add(rValue);
tmp.SelectedImageIndex = 2;
tmp.ImageIndex = 2;
AssociateTag(tmp, reader.LineNumber);

AttrNode.SelectedImageIndex = 2;
AttrNode.ImageIndex = 2;

}

if (bIsEmpty)
WORKINGNODE = WORKINGNODE.Parent;

break;
case XmlNodeType.Text:
{
string rValue = reader.Value.Replace("\r\n", " ");
newNode = WORKINGNODE.Nodes.Add(rValue);
AssociateTag(newNode, reader.LineNumber);
newNode.SelectedImageIndex = 2;
newNode.ImageIndex = 2;
}
break;
case XmlNodeType.EndElement:
WORKINGNODE = WORKINGNODE.Parent;
break;
}
}
reader.Close();
RootNode.Expand();

}
catch (Exception eee)
{
Console.WriteLine(eee.Message);
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
XML tree  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to configure session state
Previous Resource: Generate XML Response
Return to Discussion Resource Index
Post New Resource
Category: XML


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use