How to bind xml elements to the GridView using the xmlDataSourse?

萝らか妹 提交于 2019-12-25 02:53:34

问题


I have a service returning xmlData in a string format:

    <CourseSearchDataSet xmlns="http:/www.tempuri.org/CourseSearchDataSet.xsd">
     <Course>
       <CourseId>9e980791</CourseId>
       <CourseName>Library and Information Solutions</CourseName>
       <CourseDesc>Overview of Library</CourseDesc>
     </Course>
     <Course>
       <CourseId>f3e59e56</CourseId>
       <CourseName>Power Link Intro</CourseName>
       <CourseDesc>This course</CourseDesc>
     </Course>
</CourseSearchDataSet>

At runtime I am trying to bind this data to GridView using the xmlDataSource.

I have defined a grid and dataSource at the design time:

<asp:GridView runat="server" ID="grdCourses" XPath="Course/">
            <Columns>
                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                          <%# XPath("CourseId") %>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    <asp:XmlDataSource runat="server" ID="xmlDataSource"></asp:XmlDataSource>

At design time I am assigning the data to the xmlDataSource and assigning xmlDataSource to grid:

 xmlDataSource.EnableCaching = false;
        xmlDataSource.DataFile =  courseList;
        grdCourses.DataSourceID = "xmlDataSource";

("courseList" is a string holding service result).

The grid has been binded to the xmldDataSource but its not displaying the CourseId (the fields defined in templateField, no error, just blank columns.

I am referring to this tutorial: http://goo.gl/mUHkYm I don't want to use DATASET since its messy and requires many events to be handled manually.

Any help would be highly appreciated.

Thanks in advance. Vishal


回答1:


I think it's just your namespace which was causing problem, try below code .

 protected void Page_Load(object sender, EventArgs e)
        {
            XPathDocument document = new XPathDocument(@"D:\xxxx\xxx\xx\xxx\test.xml");
            XPathNavigator navigator = document.CreateNavigator();
            XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
            manager.AddNamespace("bk", "http:/www.tempuri.org/CourseSearchDataSet.xsd");
        } 

aspx:

<form id="form1" runat="server">
    <div>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="/test.xml">
</asp:XmlDataSource>
    <asp:GridView runat="server" ID="grdCourses" AutoGenerateColumns="false" XPath="bk:courseSearchDataSet/bk:Course" DataSourceID="XmlDataSource1">
            <Columns>
                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                          <%# XPath("CourseId") %>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>       
    </div>
    </form>



回答2:


If you have XML string data instead of XML file, you should bind the XML string to XmlDataSource by setting Data property instead of DataFile :

xmlDataSource.EnableCaching = false;
xmlDataSource.Data =  courseList;
grdCourses.DataSourceID = "xmlDataSource";
dataSource.XPath = "....";
dataSource.DataBind();

Related question : ASP.NET 3.5 bind to XML string



来源:https://stackoverflow.com/questions/23387894/how-to-bind-xml-elements-to-the-gridview-using-the-xmldatasourse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!