Save Listview items to Dataset > XML

J

JamesB

I have found plenty of details on filling a listview from a dataset, but I
need to do the opposite. I have a ListView and I would like to save out the
information it contains to xml (or anything else I guess) that can then
easily be imported into Excel for example.

Any pointers? I see DataSets have a WriteXML method which should be fine,
I've used it before, but I can't figure out how to get the Listview contents
into the dataset!

Thanks
 
M

Marc Gravell

So just reverse the logic... I've used DataTable here, but "csv" or
"tsv" would be better choices if Excel is your intent (I didn't spot
this until just now) - plus they're very easy to construct (much
easier than DataTable)...

using System;
using System.Windows.Forms;
using System.Xml;
using System.Data;

class Program {
static void Main() {
Application.EnableVisualStyles();
using(Form f = new Form())
using (ListView lv = new ListView())
using (Button b = new Button()) {
lv.Columns.Add("Bloop");
lv.Columns.Add("Blap");
lv.Columns.Add("Blip Blop");
lv.View = View.Details;
int j = 1;
for (int i = 0; i < 10; i++) {
string[] items = {j++.ToString(), j++.ToString(),
j++.ToString()};
lv.Items.Add(new ListViewItem(items));
}

b.Click += delegate {
DataTable dt = new DataTable("MyData");
foreach (ColumnHeader col in lv.Columns) {
dt.Columns.Add(col.Text, typeof(string));
}
object[] vals = new object[lv.Columns.Count];
foreach (ListViewItem item in lv.Items) {
ListViewItem.ListViewSubItemCollection items =
item.SubItems;
for (int i = 0; i < vals.Length; i++) {
vals = items.Text;
}
dt.Rows.Add(vals);
}
dt.WriteXml(@"c:\out_dt.xml");
};
b.Dock = DockStyle.Top;
lv.Dock = DockStyle.Fill;
f.Controls.Add(b);
f.Controls.Add(lv);
Application.Run(f);
}
}
}
 
J

JamesB

Marc Gravell said:
So just reverse the logic... I've used DataTable here, but "csv" or "tsv"
would be better choices if Excel is your intent (I didn't spot this until
just now) - plus they're very easy to construct (much easier than
DataTable)...

using System;
using System.Windows.Forms;
using System.Xml;
using System.Data;

class Program {
static void Main() {
Application.EnableVisualStyles();
using(Form f = new Form())
using (ListView lv = new ListView())
using (Button b = new Button()) {
lv.Columns.Add("Bloop");
lv.Columns.Add("Blap");
lv.Columns.Add("Blip Blop");
lv.View = View.Details;
int j = 1;
for (int i = 0; i < 10; i++) {
string[] items = {j++.ToString(), j++.ToString(),
j++.ToString()};
lv.Items.Add(new ListViewItem(items));
}

b.Click += delegate {
DataTable dt = new DataTable("MyData");
foreach (ColumnHeader col in lv.Columns) {
dt.Columns.Add(col.Text, typeof(string));
}
object[] vals = new object[lv.Columns.Count];
foreach (ListViewItem item in lv.Items) {
ListViewItem.ListViewSubItemCollection items =
item.SubItems;
for (int i = 0; i < vals.Length; i++) {
vals = items.Text;
}
dt.Rows.Add(vals);
}
dt.WriteXml(@"c:\out_dt.xml");
};
b.Dock = DockStyle.Top;
lv.Dock = DockStyle.Fill;
f.Controls.Add(b);
f.Controls.Add(lv);
Application.Run(f);
}
}
}



That seemed to work a treat!
Thanks

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top