Hi ,
i have a listview with column & item
i need to save those to an xml file so after i can manipulate with database
i searched here on the forum before found some example but not so good
i just search for a simple one
Thanks :)
Printable View
Hi ,
i have a listview with column & item
i need to save those to an xml file so after i can manipulate with database
i searched here on the forum before found some example but not so good
i just search for a simple one
Thanks :)
any one ? :rolleyes:
is it a hard question or something may i cant know ?
i can save the items to a .txt file
but i want to save it to .xml file to interact with dataset....
Thanks
Try creating a List(Of ListViewItem) which will hold all of the items and then apply XML serialization.
Thanks m8 but do you have any example i didnt get it well ?Quote:
Originally Posted by obi1kenobi
Hmmm... Just checked MSDN documentation and it states that ArrayLists and List(Of T) cannot be serialized using the XmlSerializer. No matter, you should still be able to serialize an array of ListViewItems.
This is how you would do it:
- Create a class which would have an array of ListViewItems as a variable.
Code:Public Class ListViewItemHolder
Public items() As ListViewItem
End Class
- Fill the array in your newly created class with your ListViewItem objects.
- Serialize the class using an instance of the System.Xml.Serialization.XmlSerializer class. If you have issues with the use of this class, the MSDN Library has an excellent example in the Examples section of this page.
If you need further assistance, I can try to write a complete example in VS for you in the morning.
Quote:
Originally Posted by obi1kenobi
Thanks m8 For your help much appreciated
it would be nice if you can write a complete example in VS
but when you have free time
it's now 2 days & i'am trying some stuff about ListViewItem & others but no luck :rolleyes:
Thanks :)
So you didn't get it working yet huh? Ok I'll try and write an example... Be back shortly.
Argh... The Font property cannot be serialized because Font does not have a parameterless constructor. :@
Ok, change of strategy. Which parts of the ListViewItem do you need? The main text, any subitems, what else? You can create a structure which will hold the relevant details and serialize an array of that structure. Just tell me what you need and I'll complete the example.
Hi ,Quote:
Originally Posted by obi1kenobi
i have different data coming each time
but let's say an easy example i can get with it so i can update it to my own
we have a listview1 with 3 column 1 : customer_id,customer_name,customer_email
item subitem1 subitem 2
1 John [email protected]
2 Brad [email protected].
so when i save it to an .xml file it will be look like this
Thanks :)Code:<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<customer>
<customer_id>1</customer_id>
<customer_name>John</customer_name>
<customer_email>[email protected]</customer_email>
</customer>
<customer>
<customer_id>2</customer_id>
<customer_name>Brad</customer_name>
<customer_email>[email protected]</customer_email>
</customer>
</NewDataSet>
you can serialize a list(of T).
heres how to serialize/deserialize a listviews contents
vb Code:
Public Structure ListViewItems Public column1 As String Public column2 As String Public column3 As String Public column4 As String End Structure Private items As New List(Of ListViewItems) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click items = New List(Of ListViewItems) For Each lvi As ListViewItem In ListView1.Items Dim lv As New ListViewItems lv.column1 = lvi.SubItems(0).Text lv.column2 = lvi.SubItems(1).Text lv.column3 = lvi.SubItems(2).Text lv.column4 = lvi.SubItems(3).Text items.Add(lv) Next Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of ListViewItems))) Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create) serializer.Serialize(fs, items) fs.Close() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load items = New List(Of ListViewItems) Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of ListViewItems))) Dim SR As New IO.StreamReader("test.xml") items = DirectCast(serializer.Deserialize(SR), Global.System.Collections.Generic.List(Of ListViewItems)) SR.Close() For x As Integer = 0 To items.Count - 1 Dim lvi As New ListViewItem(items(x).column1) lvi.SubItems.Add(items(x).column2) lvi.SubItems.Add(items(x).column3) lvi.SubItems.Add(items(x).column4) ListView1.Items.Add(lvi) Next End Sub
That's exactly what I was thinking. Does it run properly? Have you checked it? I'd only replace the manual addition of subitems with a loop, so no subitems get lost.
About the List(Of T) thing, I can't imagine why MSDN would write an incorrect statement... :/
yep. tried + tested + it worksQuote:
Originally Posted by obi1kenobi
Hi paul ,Quote:
Originally Posted by .paul.
Thanks m8 for your example it work great but just having some problem here
2 problem :
i'am trying instead of calling the column column1,column2 .. to Listview1.column original that are
i'am trying to loop the subitem because each time i can have 2 column or 3 or 4..
here you my code :
hope you understand what i meanCode:Public Structure ListViewItems
Public col1 As String
Public col2 As String
Public col3 As String
Public col4 As String
End Structure
Private items As New List(Of ListViewItems)
Private Sub btnsavexml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsavexml.Click
items = New List(Of ListViewItems)
Dim i As Integer
Dim lv As New ListViewItems
For Each lvi As ListViewItem In ListView1.Items
For i = 0 To ListView1.Columns.Count - 1
lv.col1 = lvi.SubItems(i).Text
items.Add(lv)
Next
Next
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of ListViewItems)))
Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
serializer.Serialize(fs, items)
fs.Close()
End Sub
Thanks ;)
you can't loop it that way.
i'd use the 4 column approach. if only 2 or 3 columns are used, you'll have 1 or 2 empty fields in your xml record. its the only way if some of your listviewitems have 4 columns.
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click items = New List(Of ListViewItems) For Each lvi As ListViewItem In ListView1.Items Dim lv As New ListViewItems lv.column1 = lvi.SubItems(0).Text If lvi.SubItems.Count > 1 Then lv.column2 = lvi.SubItems(1).Text If lvi.SubItems.Count > 2 Then lv.column3 = lvi.SubItems(2).Text If lvi.SubItems.Count > 3 Then lv.column4 = lvi.SubItems(3).Text items.Add(lv) Next Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of ListViewItems))) Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create) serializer.Serialize(fs, items) fs.Close() End Sub
The problem is that an item may not have subitems for all columns. :S
IMHO your loops should look like this:
Code:For Each lvi As ListViewItem In ListView1.Items
For i = 0 To lvi.SubItems.Count - 1
lv.SubItems.Add(lvi.SubItems(i))
Next
items.Add(lv)
Next
Thank you both Guy i got it working but still a problem that you didnt answered me
how i can change the column1,column2,column3,column4 name to original listview column name ?
cheerz :)
just change all occurrences of column1 to the original listview column name, then do the same for column2,column3,column4.
you could also change all occurrences of ListViewItems to another name.
if you open the xml file, you'll see how those names are used.
what do you mean by occurrences :rolleyes: ?Quote:
Originally Posted by .paul.
definition of occurrence
lol i changed that i was thinking something else because it doesnt work really :blush:Quote:
Originally Posted by .paul.
theres no reason it shouldn't work
yes it doesnt work :blush:Quote:
Originally Posted by .paul.
whats the problem? why doesn't it work?
post the code
here you last oneQuote:
Originally Posted by .paul.
Code:Public Structure ListViewItems
Public col1 As String
Public col2 As String
Public col3 As String
Public col4 As String
Public col5 As String
Public col6 As String
Public col7 As String
Public col8 As String
Public col9 As String
Public col10 As String
Public col11 As String
Public col12 As String
Public col13 As String
Public col14 As String
Public col15 As String
Public col16 As String
End Structure
Private items As New List(Of ListViewItems)
Private Sub btnsavexml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsavexml.Click
items = New List(Of ListViewItems)
Dim i As Integer
Dim lv As New ListViewItems
lv.col1 = ListView1.Columns.Item(0).Text
lv.col2 = ListView1.Columns.Item(1).Text
lv.col3 = ListView1.Columns.Item(2).Text
lv.col4 = ListView1.Columns.Item(3).Text
lv.col5 = ListView1.Columns.Item(4).Text
lv.col6 = ListView1.Columns.Item(5).Text
lv.col7 = ListView1.Columns.Item(6).Text
lv.col8 = ListView1.Columns.Item(7).Text
lv.col9 = ListView1.Columns.Item(8).Text
lv.col10 = ListView1.Columns.Item(9).Text
lv.col11 = ListView1.Columns.Item(10).Text
lv.col12 = ListView1.Columns.Item(11).Text
lv.col13 = ListView1.Columns.Item(12).Text
lv.col14 = ListView1.Columns.Item(13).Text
lv.col15 = ListView1.Columns.Item(14).Text
lv.col16 = ListView1.Columns.Item(15).Text
For Each lvi As ListViewItem In ListView1.Items
lv.col1 = lvi.SubItems(i).Text
If lvi.SubItems.Count > 1 Then lv.col2 = lvi.SubItems(1).Text
If lvi.SubItems.Count > 2 Then lv.col3 = lvi.SubItems(2).Text
If lvi.SubItems.Count > 3 Then lv.col4 = lvi.SubItems(3).Text
If lvi.SubItems.Count > 4 Then lv.col5 = lvi.SubItems(4).Text
If lvi.SubItems.Count > 5 Then lv.col6 = lvi.SubItems(5).Text
If lvi.SubItems.Count > 6 Then lv.col7 = lvi.SubItems(6).Text
If lvi.SubItems.Count > 7 Then lv.col8 = lvi.SubItems(7).Text
If lvi.SubItems.Count > 8 Then lv.col9 = lvi.SubItems(8).Text
If lvi.SubItems.Count > 9 Then lv.col10 = lvi.SubItems(9).Text
If lvi.SubItems.Count > 10 Then lv.col11 = lvi.SubItems(10).Text
If lvi.SubItems.Count > 11 Then lv.col12 = lvi.SubItems(11).Text
If lvi.SubItems.Count > 12 Then lv.col13 = lvi.SubItems(12).Text
If lvi.SubItems.Count > 13 Then lv.col14 = lvi.SubItems(13).Text
If lvi.SubItems.Count > 14 Then lv.col15 = lvi.SubItems(14).Text
If lvi.SubItems.Count > 15 Then lv.col16 = lvi.SubItems(15).Text
items.Add(lv)
Next
Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of ListViewItems)))
Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
serializer.Serialize(fs, items)
fs.Close()
End Sub
Thanks
vb Code:
Private Sub btnsavexml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsavexml.Click items = New List(Of ListViewItems) For Each lvi As ListViewItem In ListView1.Items Dim lv As New ListViewItems lv.col1 = lvi.SubItems(0).Text If lvi.SubItems.Count > 1 Then lv.col2 = lvi.SubItems(1).Text If lvi.SubItems.Count > 2 Then lv.col3 = lvi.SubItems(2).Text If lvi.SubItems.Count > 3 Then lv.col4 = lvi.SubItems(3).Text If lvi.SubItems.Count > 4 Then lv.col5 = lvi.SubItems(4).Text If lvi.SubItems.Count > 5 Then lv.col6 = lvi.SubItems(5).Text If lvi.SubItems.Count > 6 Then lv.col7 = lvi.SubItems(6).Text If lvi.SubItems.Count > 7 Then lv.col8 = lvi.SubItems(7).Text If lvi.SubItems.Count > 8 Then lv.col9 = lvi.SubItems(8).Text If lvi.SubItems.Count > 9 Then lv.col10 = lvi.SubItems(9).Text If lvi.SubItems.Count > 10 Then lv.col11 = lvi.SubItems(10).Text If lvi.SubItems.Count > 11 Then lv.col12 = lvi.SubItems(11).Text If lvi.SubItems.Count > 12 Then lv.col13 = lvi.SubItems(12).Text If lvi.SubItems.Count > 13 Then lv.col14 = lvi.SubItems(13).Text If lvi.SubItems.Count > 14 Then lv.col15 = lvi.SubItems(14).Text If lvi.SubItems.Count > 15 Then lv.col16 = lvi.SubItems(15).Text items.Add(lv) Next Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of ListViewItems))) Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create) serializer.Serialize(fs, items) fs.Close() End Sub
What did you changed ?
where are the listview column item?
you can't add the column names that way.
this is how:
vb Code:
Public Structure ListViewItems 'you could change this to Customers (for example) Public col1 As String 'you could change this to Customer_ID (for example) Public col2 As String 'and this to Customer_Name Public col3 As String Public col4 As String Public col5 As String Public col6 As String Public col7 As String Public col8 As String Public col9 As String Public col10 As String Public col11 As String Public col12 As String Public col13 As String Public col14 As String Public col15 As String Public col16 As String End Structure
do you see what i mean?
but you cant initialise a Structure variable :)Quote:
Originally Posted by .paul.
if you try this you will get error
and i know if i made it in this way it will workCode:
Public Structure ListViewItems
Public col1 As String = "customer_name" 'Error Here
End Structure
Private items As New List(Of ListViewItems)
but each time i have different column name so that why i want to get those name from Listview column nameCode:Public customer_id As String
hope see what i mean :)
you know the column names at design time. its easy enough to type them out
vb Code:
Public Structure Customers Public customer_name As String 'etc End Structure Private items As New List(Of Customers)
that way you'll get descriptive names in your xml file + probably get your column names when you read the xml into a dataset later.
nope & that's problem that i'am trying to solve
here the situation so to see it well :
I have 2 Form right now
Form1: getting data & there is a listview there that show column for a specific table(everytime it change)
Form2 :it get column & row from selected listview table on form1
so if i theory it should be something like this
but the problem is that it cant be written like this way :)Code:
Public Structure Form1.lstvtable.SelectedItems.Item(0).Text
Public col1 As String = Form1.lstvcolumn.SelectedItems.Item(0).Text
'the col1 should be name from lstvcolumn.SelectedItems.Item(0).Text that come from form1....
....
End Structure
Private items As New List(Of Form1.lstvtable.SelectedItems.Item(0).Text)
hope you understand me here well :)
Thanks
Why would it matter? It's only the name shown before the code is built... It might even be changed during building, so that the actual exe doesn't contain it at all... Why do you want it to be so?
Perhaps it could be done with runtime compilation, but it would most definitely be overkill in my book.
i tought it was something easy :blush:
still not getting to work
just a thought
Can't we create a dataset and then transfer it to an xml file?
for example after I copy data from listview to a dataset then wouldn't the below help?
Code:Dim LSTItms As New DataSet()
' Write XML to file
LSTItms.WriteXml("XMLFile.xml")
Hi ,Quote:
Originally Posted by koolsid
how you will transfer columns & rows from Listview to Dataset ?
i'am using xml as way to access to dataset :)
anyone has an idea ? :)
I think there is no solution for it !!
btw thanks to paul and obi1kenobi for your help :)
if you're loading the listview from a dataset, you could just write the dataset to xml, couldn't you?
where is the original data coming from?
the data are coming from website & i want to arrive this data to dataset by Xml way :)Quote:
Originally Posted by .paul.
ok. i tried a different tack. try this:
vb Code:
Dim ds As New DataSet ds.Tables.Add("customer") For x As Integer = 0 To ListView1.Columns.Count - 1 ds.Tables("customer").Columns.Add(ListView1.Columns(x).Text, GetType(System.String)) Next For Each item As ListViewItem In ListView1.Items Dim data(item.SubItems.Count - 1) As Object For x As Integer = 0 To item.SubItems.Count - 1 data(x) = item.SubItems(x).Text Next ds.Tables("customer").LoadDataRow(data, True) Next ds.WriteXml("customers.xml")