Hi ,
i have a project in which i created two classes TreeDisplay(Form.cs) and MytreeNode class in same namespace.
TreeDisplay
class contains all the GUI related stuff like Browse button
textbox,label,progress bar and TReeView. I want the user to select a
XML file through browse button which will be displayed in textbox.The
Xml file selected will be displayed in Treeview on winform.
Now i
want to separate the GUI part from business logic(where i am creating
treenode and adding nodes to tree) thats why i created the MytreeNode
class. I am passing the treeview and the string filename from the
FileOpenDialogue in Browse button.
i want to pass a Xml file name from one class to another class in same namespace using URI.i want to initialize with file path. i have passed the treeview object from GUI to my backhand class MytreeNode class.
But how to initialize with fie path? here is a part of code.
public class MytreeNodeClass
{
internal void initiatingTree(string nameofFile,TreeView treeView1)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(nameofFile);
treeView1.Nodes.Clear();
if (xmlDocument.DocumentElement != null)
if (true) treeView1.Nodes.Add(new TreeNode(xmlDocument.DocumentElement.Name));
Help me in putting up the right part of code at right place.
Thanks
Loading
Bechir BejaouiPosted Mar 9, 2009, 4:04 PM
public class XmlParser
{
public TreeView treeView;
public string Path { get; set; }
public XmlParser() { }
public XmlParser(string path , TreeView treeView)
{
this.Path = path;
this.treeView = treeView;
StreamReader oReader = new StreamReader(path);
string Content = oReader.ReadToEnd();
XElement xe = XElement.Parse(Content);
int i = 0;
List list = new List
foreach (XElement el in xe.Elements())
{
list.Add(new TreeNode(el.Name.ToString()));
foreach (XElement subel in el.Elements())
{
list[i].Nodes.Add(new TreeNode(subel.Name.ToString() + " : " + subel.Value));
}
foreach (XAttribute attr in el.Attributes())
{
list[i].Nodes.Add(new TreeNode(attr.Name.ToString() + " : " + attr.Value));
}
i++;
}
treeView.Nodes.AddRange(list.ToArray());
}
}
So what to do is to open a winfows project and add a treeview within it and ajust sizes and then call the constructor of that class by overloading it by the xml file path and the added treeview instance, any xml file will be parsed and respesented in the treeview as main and children nodes requardless of the given xml structure.