2

I have an XML document like this:

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car type="Wagon" make="Volvo">
        <colours>
            <colour>Red</colour>
            <colour>Yellow</colour>
        </colours>
    </car>
    <car type="Sedan" make="Audi">
        <colours>
            <colour>Green</colour>
            <colour>Blue</colour>
        </colours>
    </car>
</cars>

This is being generated by an ASP.NET XmlDataSource which uses an XSL file to transform the XML output of a HTTP URL.

I want to bind the XmlDataSource to an ASP.NET gridview, but when using auto-generate columns, only the type and make attributes are being bound.

I want the colours of the cars bound to a third column in CSV format like this:

Type     Make     Colours
Wagon    Volvo    Red, Yellow
Sedan    Audi     Green, Blue

I understand that could modify my transform file to store the colours in an XML attribute, but then this would make it hard to filter the XmlDataSource on colour.

I am planning on modifying the XmlDataSource's Xpath property dynamically in order the filter the data being displayed.

Is there any way I can achieve this functionality using ASP.NET. I have .NET Framework 4.0 at my disposal.

1
  • You need to you an ItemTemplate field and use something like a repeater inside it...can you post the markup of the xmldatasource. Commented Sep 8, 2012 at 15:24

1 Answer 1

2

There must definitely be an easier way, but here is something for starts...got this after a lot of digging around.

You'll need a TemplateField. Inside it you'll need to place an XmlDataSource and a repeater.

Point the xmldatasource to the file in consideration. Specify the same http url and xslt transformation mentioned before. In the XPath you'll need to select the node taking all the attributes in the car node into consideration

So for the first xmldatasource the Xpath would look like

cars/car[@type='Wagon' and @make='Volvo']/colours/colour

In the repeater itemtemplate do something like:

XPath(".")

If you are interested for the code sample for this exercise:

http://www.coderun.com/ide/?w=aKnePm185kW3jo0Uzr_s3Q

Happy coding.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.