Link to home
Start Free TrialLog in
Avatar of DeonM
DeonM

asked on

Bind XMLNode to ASP.Net GridView

I'm having difficulties binding a XMLNode to gridview.
If i use a xmlnodereader and read it into a dataset, the dataset has 4 tables.
How can i bind the dataset to the gridview to show all the info from the 4 tables?
Basically i wish to display the vehicle spec information in the grid.

I have attached example of the xmlnode i retrieve from the data supplier.

Any help greatly appreciated.
xmlnode.txt
Avatar of Craig Yellick
Craig Yellick
Flag of United States of America image

GridView needs a structured table comprised of a predictable set of rows and columns. If your XML is not strictly row/column oriented then you'll have to do some pre-processing to get it into shape. The easiest way to accomplish this is to run an XSL-T stylesheet transformation against the original XML, to make it perfect for use with GridView.

The XML sample you provided looks pretty straightforward so this should not be a problem.
Guess I should provide an example.  Here's the resulting pattern of name=value elements generated by the XSL-T below.

  <CarInfo>
    <Spec>
      <Category>Engine !AMP! Gearbox</Category>
      <Name>Accel 0-100</Name>
      <Value>11.3 Seconds</Value>
    </Spec><Spec>

Essentially you get a flattened set of spec values over all categories.
<?xml version='1.0'?>
<xsl:stylesheet 
   xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
   version='1.0'>
<xsl:output method='xml'/>
 
<xsl:template match='/'>
  <CarInfo>
   <xsl:apply-templates select='//Spec' />	
  </CarInfo>
</xsl:template>
 
<xsl:template match='Spec'>
  <Spec>
    <Category>
      <xsl:value-of select='../@name'/>
    </Category>
    <Name>
      <xsl:value-of select='@name'/>
    </Name>
    <Value>
      <xsl:value-of select='text()'/>
    </Value>
  </Spec>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

Avatar of DeonM
DeonM

ASKER

Thanks for your reply, how i understand this is, I saved the example as my schema file and read it into my dataset. But i receive and error on the ReadXML line.

I receive the following error:
"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."

Thanks for your help, this XML confuses me.
string xmlstring = SessionValues.VehicleSpec.OuterXml.ToString();
                DataSet ds = new DataSet();
                System.IO.StringReader rdr = new System.IO.StringReader(xmlstring);
                ds.ReadXmlSchema("\\xmlschema.xsd");
                ds.ReadXml(rdr);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Craig Yellick
Craig Yellick
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial