Results 1 to 7 of 7

Thread: Listview to XML file create

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    60

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    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.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Listview to XML file create

    Also, if you're getting an error, it would help to know what the error is.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.   Dim myListViewData As New DataTable("ErrorLVData")
    3.  
    4.   'Add a Column in a DataTable for each Column in the ListView
    5.   With myListViewData
    6.     .Columns.Add(New DataColumn("Col1", GetType(String)))
    7.     .Columns.Add(New DataColumn("Col2", GetType(String)))
    8.     'etc
    9.   End With
    10.  
    11.   For Each LVI As ListViewItem In ListView1.Items
    12.     Dim newDataRow As DataRow = myListViewData.NewRow
    13.     'Add each Column of the ListView to the corresponding Column in the DataTable
    14.     With newDataRow
    15.       .Item(0) = LVI.Text
    16.       .Item(1) = LVI.SubItems(1).Text
    17.     End With
    18.     myListViewData.Rows.Add(newDataRow)
    19.   Next
    20.  
    21.   'Finally, just export the DataTable to an XML File
    22.   myListViewData.WriteXml("d:\temp\YourXMLFieName.xml", XmlWriteMode.WriteSchema)
    23. End Sub

    Hope that helps.

    Cheers,

    Ian

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    60

    Re: Listview to XML file create

    Thanks JMC, techgnome n Ian.....

    below image is Listview control want to save as xml file
    bName:  listviewError.jpg
Views: 1207
Size:  174.2 KB


    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.ObjectByVal e As System.EventArgsHandles 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...

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    60

    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
  •  



Click Here to Expand Forum to Full Width