Solved

Bind XMLNode to ASP.Net GridView

Posted on 2009-03-31
4
1,486 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:DeonM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 11

Expert Comment

by:CraigYellick
ID: 24029860
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.
0
 
LVL 11

Expert Comment

by:CraigYellick
ID: 24030149
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

0
 

Author Comment

by:DeonM
ID: 24047814
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

0
 
LVL 11

Accepted Solution

by:
CraigYellick earned 500 total points
ID: 24049049
I did not post a complete solution. There's quite a bit more work to do. I posted an example of one way to solve your problem using an XSL transform.

The XSL transform code should be applied to the original XML data, and the results of the transform are then loaded into a dataset that is designed specifically for the transformed XML.

   Original XML -> through XSL -> New XML -> into DataSet

To use your existing code, you'll need to create a new schema XSD for that new XML. The following web page shows how to do this. You'll need an example of the XML generated by the XSL transform. You may need to adjust the resulting XML to better match your needs.

   http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx
0

Featured Post

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question