网格视图的 Linq To Xml 数据源不起作用。网格不显示行
我正在从服务中获取一些 Xml。我希望它成为我的 aspx 页面上网格视图的数据源。这是 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>
,这是我解析 xml 并尝试绑定网格的代码。我在这里缺少什么?
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("HOVOnly")
, CreateDateTime = (DateTime)t.Element("CreateDateTime")
, ConfigVTMSDelaySeconds = (int)t.Element("ConfigVTMSDelaySeconds")
};
grdTripDetails.DataSource = tripTolls;
grdTripDetails.DataBind();
我意识到这些都是匿名类型。这是一个问题吗?我已验证该服务正在返回如上所述的 Xml。有人能指出我正确的方向吗?非常感谢您的任何提示。
为了完整起见,这里是网格标记
<asp:GridView runat="server" ID="grdTripDetails" />
干杯,
〜ck在圣地亚哥
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("HOVOnly")
, 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
注意其中添加了
Root
。只有一个顶级元素,而且它不是 TripTollCompleteDC。您可能还需要自动生成列 - 我不了解网格视图。这提出了关于调试此类事情的一个有用的观点。这里有两个潜在的问题:
您可以通过日志记录来测试第一点 - 以您通常进行日志记录的任何方式记录转换后的值。哎呀,您甚至可以使用单独的控制台应用程序进行测试。 (这就是我刚刚所做的。)
您可以通过硬编码一些数据并重新加载页面来测试第二点 - 它是否按您期望的方式显示?如果没有,请调整代码,直到它满足您的要求。
将这两个问题分开可以更容易地解决问题。
Try this:
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:
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.