Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have rather strange requirement of gathering data through bunch of xml files and binding it to Listview(WPF one). this part was somehow completed . but the challenge I have is have to export the listviewitems to an Datatable and from there to Excel or csv


I googled , but with no luck ,is there any suggestions or examples ?

Just saw the replies ....I think i need to be more specific on my problem

I have bunch of files(xml ones) from which i gather cretain node values and bind it to Lisview.For this i have used XMl to linq technique and have binded them

below is snippet

C#
var MethodList = from Elements in XpedDoc.Descendants(ns + "Method")
                                 select Elements;
 foreach (var XpedElement in MethodList)
               { do something }

LstNodeView.Items.Add(ndXped);// adding to my list view  where ndXped is custom class

Now after this I want export all listitems into datatable to export into Excel file 

Hope this helps
Posted
Updated 19-Jun-13 18:58pm
v3
Comments
Ron Beyer 19-Jun-13 21:01pm    
Why the intermediate data table? Is it being displayed somewhere?
mpslnt 20-Jun-13 1:33am    
Yep....... it is being exported to excel file
koleraba 19-Jun-13 21:35pm    
I am afraid I don't understand your question. I guess you have to export the data which is bind to your ListViewItems not the actual ListViewItems. Since your ListView is generated from items read from xml I don't understand what the problem is.
mpslnt 20-Jun-13 1:34am    
I do read from about 100 Xml files .. thats the reason...

1 solution

xml to data table
C#
DataTable dt=new DataTable();
DataSet ds = new DataSet();
StringReader reader = new StringReader(Xmlstring);
ds.ReadXml(reader);
dt = ds.Tables[0];

Data Table to Xml
C#
MemoryStream str = new MemoryStream();
dt.WriteXml(str, true);
str.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(str);
string Xmlstring= sr.ReadToEnd();

Hope you will get solution ...

[Edit]Code block formatting added[/Edit]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900