none
binding datagridview to xml file RRS feed

  • Question

  •  

    Hi there. i'm a newbie to xml and windows forms.

    trying to load an xml file into a datagridview.  i dropped a datagridview control on to my form and didn't set any properties.  trying to do it all programmatically.  i need to load an xml file - which i found out has some crlf embedded in it.  after stripping them out, i'm trying to bind the xml to the datagrid.  i don't get any errors but the data doesn't appear on the control.  what else do i need to do?  also after displaying the data in the grid view, i need to ultimately save this data into 2 locations:  first in a CSV format for excel, and then in a sql database.  is there anything that i should do in this code to make my life easier later?  thanks.

     

     

    Dim w3datainputfile As New XmlDocument

    If Not (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then

    'MsgBox("problems with the Open File Dialog. Contact your administrator.")

    Exit Sub

    End If

    Dim tempstring As String

    '2007.07.30 Enable MultiFile Selects

    Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)

    tempstring = sr.ReadToEnd.ToString

    tempstring = Replace(tempstring, Chr(10), "")

    w3datainputfile.LoadXml(tempstring)

    DataGridView1.DataSource = w3datainputfile

     

     

     

    Friday, August 3, 2007 2:21 AM

Answers

  •  

    Dear dotolee,

     

    Check this.

     

    HTH,

    Suprotim Agarwal

    Friday, August 3, 2007 3:10 AM
  • Hi, dotolee,

     

    The reason for your problem is that you put the "<NAME>" and "<ADDRESS>"

    nested in the "<LISTINGS>" segment which makes your "<NAME>" and "<ADDRESS>" new DataTables for you DataSet.

     

    You can try to paste the schema into a xsd file, and you can find out that the relationship between the three tables from the visual window.

    As you can see, in the "LISTINGS" table, there are only "PHONE" and "LISTION_ID" field. And the Name and Address are in the other tables.

     

    Here is the solution,

    1. You can put the Name and Address info into the <LISTINGS>, as they are one to one relationship, it is not necessary to put them in special tags.

     

    2. You can join the three tables together manually(line by line).

     

    Hopes this helps,

    Regards

    Monday, August 6, 2007 5:52 AM

All replies

  • Dear dotolee,

     

    A solution posted by Darrel :

     

    XmlNode node;
    node = GetNode();
    XmlNodeReader nr = new XmlNodeReader
    (node);
    DataSet ds = new DataSet
    () ;
    ds.ReadXml(nr);
    this
    .dataGridView1.DataSource = ds;
    nr.Close();

     

    Check more at this thread

     

    HTH,

    Suprotim Agarwal

    Friday, August 3, 2007 2:31 AM
  • hi there. thanks for the response.  i checked out the other thread too.. but i have the same question as the last guy that posted... not sure where you're reading in the xml file in your sample code.  how would i actually integrate this into my solution?

    Please and thanks.

     

    Friday, August 3, 2007 2:50 AM
  •  

    Dear dotolee,

     

    Check this.

     

    HTH,

    Suprotim Agarwal

    Friday, August 3, 2007 3:10 AM
  • hi again.  thank you for that link!  i managed to get something appear on the datagrid but not all the fields from the xml data is being pulled in.

    my code for the ReadXML button now looks like this:

     

    If Not (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then

    'MsgBox("problems with the Open File Dialog. Contact your administrator.")

    Exit Sub

    End If

    Dim filePath As String = OpenFileDialog1.FileName

    HHDataSet.ReadXml(filePath)

    DataGridView1.DataSource = HHDataSet

    DataGridView1.DataMember = "listing"

     

    but it only pulls in 2 fields in the table - phone and listing ID.  the xml data looks like this:

     

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <RESULTS>
      <INFO>
        <VERSION>2.0</VERSION>
        <STATUS>OK</STATUS>
        <SEARCH_ID>20281431433717382367</SEARCH_ID>
        <SEARCH_TYPE>Find_Person</SEARCH_TYPE>
        <COUNT>115</COUNT>
      </INFO>
      <LISTINGS>
        <LISTING>
          <NAME>
            <FIRST>A</FIRST>
            <LAST>Abbas</LAST>
          </NAME>
          <ADDRESS>
            <NUM>25</NUM>
            <STREET>Cosburn</STREET>
            <CITY>Toronto</CITY>
            <STATE>ON</STATE>
            <ZIP>M4K 3Y4</ZIP>
            <COUNTRY>Canada</COUNTRY>
          </ADDRESS>
          <PHONE>4164298763</PHONE>
          <LISTING_ID>1</LISTING_ID>

        </LISTING>

    Any suggestions on how I can get all details for each listing to appear in the grid?  I printed the schema and it looks like this:

    <?xml version="1.0" encoding="utf-16"?>
    <xsTongue Tiedchema id="RESULTS" xmlns="" xmlns:xs xmlns:msdata="urnTongue Tiedchemas-microsoft-com:xml-msdata">
      <xs:element name="RESULTS" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="INFO">
              <xs:complexType>
                <xsTongue Tiedequence>
                  <xs:element name="VERSION" type="xsTongue Tiedtring" minOccurs="0" />
                  <xs:element name="STATUS" type="xsTongue Tiedtring" minOccurs="0" />
                  <xs:element name="SEARCH_ID" type="xsTongue Tiedtring" minOccurs="0" />
                  <xs:element name="SEARCH_TYPE" type="xsTongue Tiedtring" minOccurs="0" />
                  <xs:element name="COUNT" type="xsTongue Tiedtring" minOccurs="0" />
                </xsTongue Tiedequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="LISTINGS">
              <xs:complexType>
                <xsTongue Tiedequence>
                  <xs:element name="LISTING" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                      <xsTongue Tiedequence>
                        <xs:element name="PHONE" type="xsTongue Tiedtring" minOccurs="0" />
                        <xs:element name="LISTING_ID" type="xsTongue Tiedtring" minOccurs="0" />
                        <xs:element name="NAME" minOccurs="0" maxOccurs="unbounded">
                          <xs:complexType>
                            <xsTongue Tiedequence>
                              <xs:element name="FIRST" type="xsTongue Tiedtring" minOccurs="0" />
                              <xs:element name="LAST" type="xsTongue Tiedtring" minOccurs="0" />
                            </xsTongue Tiedequence>
                          </xs:complexType>
                        </xs:element>
                        <xs:element name="ADDRESS" minOccurs="0" maxOccurs="unbounded">
                          <xs:complexType>
                            <xsTongue Tiedequence>
                              <xs:element name="NUM" type="xsTongue Tiedtring" minOccurs="0" />
                              <xs:element name="STREET" type="xsTongue Tiedtring" minOccurs="0" />
                              <xs:element name="CITY" type="xsTongue Tiedtring" minOccurs="0" />
                              <xs:element name="STATE" type="xsTongue Tiedtring" minOccurs="0" />
                              <xs:element name="ZIP" type="xsTongue Tiedtring" minOccurs="0" />
                              <xs:element name="COUNTRY" type="xsTongue Tiedtring" minOccurs="0" />
                            </xsTongue Tiedequence>
                          </xs:complexType>
                        </xs:element>
                      </xsTongue Tiedequence>
                    </xs:complexType>
                  </xs:element>
                </xsTongue Tiedequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xsTongue Tiedchema>

    Please and thanks.

    Friday, August 3, 2007 2:32 PM
  • it looks like the complex types didn't get bound to the grid.  only the strings.

    trying to google information on this but not having any success. if you have any suggestions, they'd be much appreciated.

    thank you.

     

    Friday, August 3, 2007 3:33 PM
  • Hi, dotolee,

     

    The reason for your problem is that you put the "<NAME>" and "<ADDRESS>"

    nested in the "<LISTINGS>" segment which makes your "<NAME>" and "<ADDRESS>" new DataTables for you DataSet.

     

    You can try to paste the schema into a xsd file, and you can find out that the relationship between the three tables from the visual window.

    As you can see, in the "LISTINGS" table, there are only "PHONE" and "LISTION_ID" field. And the Name and Address are in the other tables.

     

    Here is the solution,

    1. You can put the Name and Address info into the <LISTINGS>, as they are one to one relationship, it is not necessary to put them in special tags.

     

    2. You can join the three tables together manually(line by line).

     

    Hopes this helps,

    Regards

    Monday, August 6, 2007 5:52 AM
  • Thanks.  I thought that i was messing something up, being a newcomer to .net and xml. 

    If the xml files were going to be used somewhere down the road, i'd invest the time to reformat them per your message below.  but in fact, they will just be used once to extract all data and dump them into excel, and then ultimately a SQL database (that's phase 2).

    so...  i ended up creating 3 different grids.  one for each complex data type, and the other for the two strings.  seems a bit cumbersome but it was the only way i could get it running.

    Thank you very much for confirming that i'm not smoking drugs!  I was feeling a little silly for not being able to figure this out.

     

    Thanks again.

     

     

    Monday, August 6, 2007 2:35 PM