Listing an XML using LINQ

Posted by Naimishforu under C# on 3/10/2011 | Points: 10 | Views : 1869 | Status : [Member] [MVP] | Replies : 3
Hi,

I have one XML file ready with me.

I want to show it to the end user as it can be seen in notepad (XML Tree Format).

I may use TreeView or similar things.

My application is windows based and build in C#.

Is there any easy way to achieve the same??

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/



Responses

Posted by: T.saravanan on: 3/10/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Are you save the XMl file into Notepad?
If yes, Refer this code...

Namespace :  Using System.IO;

//Read the XML File
StreamReader srObj = new StreamReader(@"C:\Test.xml");
string sRead = srObj.ReadToEnd();
// Write into Text File
StreamWriter swObj = new StreamWriter(@"C:\Tests.txt");
swObj.Write(sRead);


try this...

Thanks,
T.Saravanan

Naimishforu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 3/10/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Thanks, but I want to populate it to the TreeView control, not just want to read it!!

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Naimishforu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 3/10/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Refer this code for populate in TreeView..
Here i am read the XML file into DataSet..
Consider about Column Name is Parent and that column value is Child...
TreeView trView = new TreeView();

DataSet ds = new DataSet();
// Read the XML File
ds.ReadXml(@"C:\Test.xml");
// Create TreeView
for(int c=0;c < ds.Tables[0].Columns.Count;c++)
{
TreeNode trp = new TreeNode();
trp.Text = ds.Tables[0].Columns[c].ColumnName.ToString();
for(int r=0; r < ds.Tables[0].Rows.Count; r++)
{
TreeNode trc = new TreeNode();
trc.Text = ds.Tables[0].Rows[r][c].ToString();
trp.Nodes.Add(trc);
}
trView.Nodes.Add(trp);
}


Try this....

Thanks,
T.Saravanan

Naimishforu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response