0

XML

<Parent Name="Jodi">
<Children Name="xxx" age="20">
<Children Name="zzz" age="21">
</Parent>

<Parent Name="John">
<Children Name="aaa" age="18">
<Children Name="bbb" age="17">
</Parent>

C#:

//Loading the xml file
var xmlDoc=Xdocument.Load(xmlpath);
//Querying the names of the parents from the above xml file.
var parentlist=(from p in xmlDoc.Descendants("Parent")
                select p.Attribute("Name").value.ToString();
List<string> PList= new List<string>();
PList=parentList.ToList();

//passing to list to the gridview in asp.net web page:
 gvParentsList.DataSource=PList;
 gvParentsList.DataBind();

Asp.net:

<asp:GridView runat="server" ID="gvParentsList" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Parents Name">
<ItemTemplate>
<asp:Label ID="lblParent" runat="server" Text='<%#Bind(PList) %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

上記のコードから名前を照会することはできますが、グリッドビューに表示できません。

ここでキャッチを逃している正確な場所を誰かが案内できますか?

4

1 に答える 1

0

これを試して:

        var xmlPath = @"C:\Projects\Research\StackOverflow\StackOverflow\test.xml";

        // Loading the xml file
        var xmlDoc=XDocument.Load(xmlPath);

        // Querying the names of the parents from the above xml file.
        var parentList = from p in xmlDoc.Descendants("Parent") select new { Name = p.Attribute("Name").Value };

        var PList=parentList.ToList();

        //passing to list to the gridview in asp.net web page:
         gvParentsList.DataSource = PList;
         gvParentsList.DataBind();

次に、次のようにバインドします。

<asp:Label ID="lblParent" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
于 2012-07-26T20:05:49.247 に答える