开发者

Linq To Xml datasource for grid view is not working. The grid show no rows

I'm getting some Xml back from a service. I would like it to be the datasource of a grid view on my aspx page. Here is a sample of the Xml

  <?xml version="1.0" encoding="utf-16" ?> 
<ArrayOfTripTollCompleteDC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <TripTollCompleteDC>
     <TripTollId>5</TripTollId> 
     <DMSLaneModeID xsi:nil="true" /> 
     <HOVOnly>false</HOVOnly> 
     <CreateDateTime>2010-06-07T15:54:01.023</CreateDateTime> 
     <ConfigVTMSDelaySeconds>5</ConfigVTMSDelaySeconds> 
   </TripTollCompleteDC>

and here is my code that parses the xml and tries to bind the grid. What am I missing here?

var retVal = service.GetTripDetailsByTripID(tripId);

            var xmlTrips = XDocument.Parse(retVal);
            var tripTolls =
                from t in xmlTrips.Elements("TripTollCompleteDC")
                select new {
                    TripTollId = (int)t.Element("TripTollId")
                    , DMSLaneModeID = (int?)t.Element("DMSLaneModeID")
                    , HOVOnly = (bool)t.Element("HOV开发者_运维知识库Only")
                    , CreateDateTime = (DateTime)t.Element("CreateDateTime")
                    , ConfigVTMSDelaySeconds = (int)t.Element("ConfigVTMSDelaySeconds")
                };

            grdTripDetails.DataSource = tripTolls;
            grdTripDetails.DataBind();

I realize these are anonymous types. Is that a problem? I have verified the service is returning the Xml as stated above. Can anyone out there point me in the right direction? Thanks so much for any tips.

Just for completeness, here is the grid markup

 <asp:GridView runat="server" ID="grdTripDetails" />

Cheers,

~ck in San Diego


Try this:

 from t in xmlTrips.Root.Elements("TripTollCompleteDC")

Note the addition of Root in there. There's only one top-level element, and it's not a TripTollCompleteDC.

You may also need to autogenerate the columns - I don't know about grid views. That raises a useful point about debugging this sort of thing. There are two potential problems here:

  • Parsing and transforming the XML
  • Generating the grid view

You can test the first point via logging - log the transformed values in whatever way you normally do logging. Heck, you could even just use a separate console app for testing. (That's what I've just done.)

You can test the second point by hard-coding some data and reloading the page - does it show up how you expect it to? If not, tweak the code until it does what you want.

Separating these two concerns makes it much easier to work out the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜