locked
Bind XML file to GridView problem RRS feed

  • Question

  • User-1345623911 posted

    Hi

    please take a look at this article which Binds XML file to a GridView

    http://www.magic-dev.com/bind_gridview_to_xml_file.htm

    everything is ok but my xml looks like this. it has an extra node inside and the code does not work with it

    <contacts>

    <newContacts>
      <contact name="Ibrahim Abu Marasa" phone="76576" />
      <contact name="Mohammed" phone="65346362" />

    <newContacts>

    <oldContacts>
      <contact name="Ibrahim Abu Marasa" phone="76576" />
      <contact name="Mohammed" phone="65346362" />

    <oldContacts>
    </contacts>

    Tuesday, January 22, 2013 11:44 AM

Answers

  • User3866881 posted

    i know it was just a sample

    what should the code look like!

    You have to use XDocument to read out properties and do binding:

    var result = from item in XDocument.Load("xxx.xml").Descedants("contact")
                       select new
                       {
                                Name = item.Attribute("name").Value,
                                Phone = item.Attribute("phone").Value
                       };

    Then bind the result to GridView.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 24, 2013 4:18 AM

All replies

  • User3866881 posted

    Hi,

    Your Xml isn't correct in its defination.

    Please change to this:

    <contacts>
    
    <newContacts>
       <contact name="Ibrahim Abu Marasa" phone="76576" />
       <contact name="Mohammed" phone="65346362" />
    </newContacts>
    
    <newContacts>    <contact name="Ibrahim Abu Marasa" phone="76576" />
       <contact name="Mohammed" phone="65346362" />
    </newContacts>  </contacts>
    
    

    Then you can use DataTable or DataSet.ReadXml to load the xml contents and then do what you want.

    Wednesday, January 23, 2013 9:32 PM
  • User-1345623911 posted

    yes i know the xml in my code is correct

    please keep to the subject it seems noone just reads the question!

    Thursday, January 24, 2013 2:41 AM
  • User3866881 posted

    please keep to the subject it seems noone just reads the question!

    Hi,

    Again I said that your node of your xml isn't right. You have to rechange it and do binding.

    Thursday, January 24, 2013 3:23 AM
  • User-1345623911 posted

    i know it was just a sample

    what should the code look like!

    Thursday, January 24, 2013 4:04 AM
  • User3866881 posted

    i know it was just a sample

    what should the code look like!

    You have to use XDocument to read out properties and do binding:

    var result = from item in XDocument.Load("xxx.xml").Descedants("contact")
                       select new
                       {
                                Name = item.Attribute("name").Value,
                                Phone = item.Attribute("phone").Value
                       };

    Then bind the result to GridView.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 24, 2013 4:18 AM