-
Oct 8th, 2013, 12:32 AM
#1
Thread Starter
Member
Listview to XML file create
Hello Genius please Help....
Listview to XML file create
i want to create XML file from a Listview Control
if listview control has one item then, output xml file was working fine
but listview control has more than one items then output xml file has error
not opening in iexplorer or any other browser
but opened with notepad with no xml format
contains as normal string....
thats my code
PHP Code:
Dim _xelement As XElement = <ErrorDetect>
<%= From _itms As ListViewItem In _errorVirtualListview.Items
Let fields = _itms
Select _
<Errors>
<Time><%= Now.ToString %></Time>
<NameSpace>=<%= fields.Text %></NameSpace>
<MethodName>=<%= fields.SubItems(1).Text %></MethodName>
<Code>=<%= fields.SubItems(2).Text %></Code>
<Description><%= fields.SubItems(3).Text %></Description>
<number><%= fields.SubItems(4).Text %></number>
<Ex><%= fields.SubItems(5).Text %></Ex>
<CustomMessages><%= fields.SubItems(6).Text %></CustomMessages>
</Errors>
%>
</ErrorDetect>
Trace.WriteLine(_xelement)
Using _xmlWritter As New XmlTextWriter("D:\xmlerrorfile.xml", Nothing)
_xmlWritter.WriteString(_xelement)
_xmlWritter.Flush()
_xmlWritter.Close()
End Using
Thanks
-
Oct 8th, 2013, 12:51 AM
#2
Re: Listview to XML file create
It would be a good idea for you to show us what you expect the XML to look like and what it actually looks like so that we know what we're looking for. Maybe shows us the original ListView with two items and then the correct and the faulty XML.
-
Oct 8th, 2013, 07:41 AM
#3
Re: Listview to XML file create
Also, if you're getting an error, it would help to know what the error is.
-tg
-
Oct 8th, 2013, 08:15 AM
#4
Re: Listview to XML file create
Hi,
I do agree with the two posts that have been made so far but if you are trying to create a new XML file from the contents of a ListView then here is just one Idea of how this can be done using a DataTable to represent the contents of the Columns in a Listview. The DataTable Class then has a WriteXml method that can be used to save the XML file as you need. i.e:-
vb.net Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myListViewData As New DataTable("ErrorLVData")
'Add a Column in a DataTable for each Column in the ListView
With myListViewData
.Columns.Add(New DataColumn("Col1", GetType(String)))
.Columns.Add(New DataColumn("Col2", GetType(String)))
'etc
End With
For Each LVI As ListViewItem In ListView1.Items
Dim newDataRow As DataRow = myListViewData.NewRow
'Add each Column of the ListView to the corresponding Column in the DataTable
With newDataRow
.Item(0) = LVI.Text
.Item(1) = LVI.SubItems(1).Text
End With
myListViewData.Rows.Add(newDataRow)
Next
'Finally, just export the DataTable to an XML File
myListViewData.WriteXml("d:\temp\YourXMLFieName.xml", XmlWriteMode.WriteSchema)
End Sub
Hope that helps.
Cheers,
Ian
-
Oct 9th, 2013, 12:43 AM
#5
Thread Starter
Member
Re: Listview to XML file create
Thanks JMC, techgnome n Ian.....
below image is Listview control want to save as xml file
b
ian sir..
i just tried your code it'z working like as charm ....really thanks for your help but how to load this xml file to my listview control
this is my updated ian codes..
PHP Code:
Public Function CreateNewErrorXMLFile(ByVal _xmlFilename$, ByVal _listview As ListView) As Boolean
Try
Dim myListViewData As New DataTable("ErrorCollectionDatas")
'Add a Column in a DataTable for each Column in the ListView
With myListViewData
.Columns.Add(New DataColumn("NameSpace", GetType(String)))
.Columns.Add(New DataColumn("MethodName", GetType(String)))
.Columns.Add(New DataColumn("ErrorCode", GetType(String)))
.Columns.Add(New DataColumn("Description", GetType(String)))
.Columns.Add(New DataColumn("ErrorNumber", GetType(String)))
.Columns.Add(New DataColumn("Exception", GetType(String)))
.Columns.Add(New DataColumn("CustomMessages", GetType(String)))
.Columns.Add(New DataColumn("Time", GetType(String)))
'etc
End With
For Each LVI As ListViewItem In _listview.Items
Dim newDataRow As DataRow = myListViewData.NewRow
'Add each Column of the ListView to the corresponding Column in the DataTable
With newDataRow
.Item(0) = LVI.Text
.Item(1) = LVI.SubItems(1).Text
.Item(2) = LVI.SubItems(2).Text
.Item(3) = LVI.SubItems(3).Text
.Item(4) = LVI.SubItems(4).Text
.Item(5) = LVI.SubItems(5).Text
.Item(6) = LVI.SubItems(6).Text
.Item(7) = LVI.SubItems(7).Text
End With
myListViewData.Rows.Add(newDataRow)
Next
'Finally, just export the DataTable to an XML File
myListViewData.WriteXml(_xmlFilename$, XmlWriteMode.WriteSchema)
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
CreateNewErrorXMLFile("D:\xmlerrorfile.xml", ListView1)
End Sub
this is the output xml file Ian sir codes..
PHP Code:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="ErrorCollectionDatas" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ErrorCollectionDatas">
<xs:complexType>
<xs:sequence>
<xs:element name="NameSpace" type="xs:string" minOccurs="0" />
<xs:element name="MethodName" type="xs:string" minOccurs="0" />
<xs:element name="ErrorCode" type="xs:string" minOccurs="0" />
<xs:element name="Description" type="xs:string" minOccurs="0" />
<xs:element name="ErrorNumber" type="xs:string" minOccurs="0" />
<xs:element name="Exception" type="xs:string" minOccurs="0" />
<xs:element name="CustomMessages" type="xs:string" minOccurs="0" />
<xs:element name="Time" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<ErrorCollectionDatas>
<NameSpace>Mphotoshop</NameSpace>
<MethodName>GetAppref</MethodName>
<ErrorCode>MPSGAPRFR</ErrorCode>
<Description>The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))</Description>
<ErrorNumber>-2147417846</ErrorNumber>
<Exception>System.Runtime.InteropServices.COMException (0x8001010A): The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
</Exception>
<CustomMessages>Photoshop is busy with an another work</CustomMessages>
<Time>10/9/2013 8:16:16 AM</Time>
</ErrorCollectionDatas>
<ErrorCollectionDatas>
<NameSpace>Mphotoshop</NameSpace>
<MethodName>IsphotoshopBusy</MethodName>
<ErrorCode>MPSISPSBC</ErrorCode>
<Description>Photoshop is busy with an another work</Description>
<ErrorNumber>5</ErrorNumber>
<Exception>System.Exception: Photoshop is busy with an another work
</Exception>
<CustomMessages>Nothing</CustomMessages>
<Time>10/9/2013 8:16:16 AM</Time>
</ErrorCollectionDatas>
<ErrorCollectionDatas>
<NameSpace>Mphotoshop</NameSpace>
<MethodName>GetAppref</MethodName>
<ErrorCode>MPSGAPRFR</ErrorCode>
<Description>The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))</Description>
<ErrorNumber>-2147417846</ErrorNumber>
<Exception>System.Runtime.InteropServices.COMException (0x8001010A): The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
</Exception>
<CustomMessages>Photoshop is busy with an another work</CustomMessages>
<Time>10/9/2013 8:16:16 AM</Time>
</ErrorCollectionDatas>
<ErrorCollectionDatas>
<NameSpace>Mphotoshop</NameSpace>
<MethodName>IsphotoshopBusy</MethodName>
<ErrorCode>MPSISPSBC</ErrorCode>
<Description>Photoshop is busy with an another work</Description>
<ErrorNumber>5</ErrorNumber>
<Exception>System.Exception: Photoshop is busy with an another work
</Exception>
<CustomMessages>Nothing</CustomMessages>
<Time>10/9/2013 8:16:17 AM</Time>
</ErrorCollectionDatas>
</NewDataSet>
i have some doubts, plz help me ...
1) how do add XMLComment
2) how many ways to create XML file in Vb.Net
3) Which is the best way to create XML file
4) In Above XML file how can i get details from particular element ie... Description, Time, ErrorCodes..... (want to show customer place)
5) I want to load this XML file into my listview control
plz sorry for the poor english
Thanks Genius...
-
Oct 9th, 2013, 01:07 AM
#6
Re: Listview to XML file create
1) how do add XMLComment
2) how many ways to create XML file in Vb.Net
3) Which is the best way to create XML file
4) In Above XML file how can i get details from particular element ie... Description, Time, ErrorCodes..... (want to show customer place)
5) I want to load this XML file into my listview control
Hi,
To answer your questions in order:-
1) You can add a comment block in an XML file with the follow Tag structure.
PHP Code:
<!-- this is a comment block in an XML file that has been added manually -->
However, since we are using the WriteXml Method of a DataTable you cannot add comments at the time of the creation of the file since you cannot inject any additional information into that single Method Call. So, using this method you need to add comments manually after the file has been created.
2) There are loads of ways to create XML files and I have shown but one example. I would suggest having a look about on the net to get more ideas.
3) I would say that the best way is whichever way you feel most comfortable with.
4) If you want to get specific elements from an XML file then one way to do so would be to read the file into an XMLDocument and then use the Method GetElementsByTagName to search for and retrieve specific elements. Have a look here:-
XmlDocument.GetElementsByTagName Method (String)
Another way is to re-read the file as described in point #5 and then grab the data that you want from either the DataTable or the ListView.
5) To do this then you just need to reverse the logic that I have already shown you to do the following:-
a) Read the XML file into a DataTable using the ReadXml Method of the DataTable
b) Add the corresponding Columns of the DataTable to the ListView
c) Copy the Data from the DataTable to the ListView.
Hope that helps.
Cheers,
Ian
-
Oct 9th, 2013, 01:21 AM
#7
Thread Starter
Member
Re: Listview to XML file create
Thanks Ian Sir ...
i will try and update here
once again thanks
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|