Need help(urgent) regarding showing XML data in TreeView.

this is my XML data....<ROOT>biodata <NAME>nix</NAME> <LAST>patel</LAST><WORK>mywork <W>x</W> <W>y</W></WORK></ROOT>

i can store this data in TreeView..using code example given in MSDN. but it also shows tags as nodes...that i dont want.

it shows like... ROOT

|-----biodata

|-----NAME

|------nix

|-----LAST

|------patel

|-----WORK

|-------mywork

|-----W

|-----x

|-----W

|-----y

---------------------------------------------------------

And i need only data instead of showing TAGS....like this

biodata

|-----nix

|-----patel

|-----mywork

|------x

|------y

So please..give me solution..here is the code from MSDN.

-----------------------------------------------------------------------

private void button1_Click(object sender, EventArgs e)

{

try

{

// SECTION 1. Create a DOM Document and load the XML data into it.

XmlDocument dom = new XmlDocument();

dom.Load(Application.StartupPath + "\\Sample.xml");

// SECTION 2. Initialize the TreeView control.

treeView1.Nodes.Clear();

treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));

TreeNode tNode = new TreeNode();

tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM nodes.

AddNode(dom.DocumentElement, tNode);

treeView1.ExpandAll();

}

catch (XmlException xmlEx) { MessageBox.Show(xmlEx.Message); }

catch (Exception ex) { MessageBox.Show(ex.Message); }

}

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)

{

XmlNode xNode;

TreeNode tNode;

XmlNodeList nodeList;

int i;

// Loop through the XML nodes until the leaf is reached.

// Add the nodes to the TreeView during the looping process.

if (inXmlNode.HasChildNodes)

{

nodeList = inXmlNode.ChildNodes;

for(i = 0; i<=nodeList.Count - 1; i++)

{

xNode = inXmlNode.ChildNodesIdea;

inTreeNode.Nodes.Add(new TreeNode(xNode.Name));

tNode = inTreeNode.NodesIdea;

AddNode(xNode, tNode);

}

}

else

{

// Here you need to pull the data from the XmlNode based on the

// type of node, whether attribute values are required, and so forth.

inTreeNode.Text = (inXmlNode.OuterXml).Trim();

}

}



Answer this question

Need help(urgent) regarding showing XML data in TreeView.

  • Deepak83

    Given <TAG>content &amp; sons></TAG>
    MyXmlNode.InnerXml would give you "content &amp; sons"
    or MyXmlNode.InnerText will give you "content & sons"
    If you used OuterXml it would give you <TAG>content &amp; sons></TAG>
    They are creating an extra node. See my first post.




  • antony leo

    Any code that creates a treenode that has the text of DocumentElement.name and xNode.Name you want to remove. This is the offending code that is adding the tags. you then have to adjust the next code accordingly.

  • assassin316

    may be i am wrong..

    but...the data between <TAG>data</TAG>..is not available in any property of node...

    can u tell me in detail..or can u change the code..PLease


  • Need help(urgent) regarding showing XML data in TreeView.