条件为真时的XML节点到表(C#,ASP.net)

时间:2015-01-15 10:59:54

标签: c# asp.net xml

我刚刚开始使用C#和ASP.Net,我需要帮助填充表格。 这就是xml的样子:

`<client>
  <person>
    <id>12345</id>
    <name>John</name>
    <surname>Smith</surname>
    <type>New</type>
   </person>
</client>` 

我想在文本字段中输入id,按下提交按钮,如果id与xml文件中的一个id匹配,则填充一个带有名称的表。到目前为止,如果我按下按钮没有任何反应。

到目前为止这是我的aspx(我相信"</asp:DataList>"应该在最后,但如果我这样做会产生错误):

  <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">



    <asp:XmlDataSource ID="SalaDataSource" runat="server" DataFile="~/App_Data/clienti.xml" XPath="/clienti/persoana" ></asp:XmlDataSource>
    <asp:DataList ID="dlPerson" runat="server" DataSourceID="SalaDataSource"> </asp:DataList>

    <h2>Verify member</h2><br/>

         <table>
                <tr>
                    <td style="padding-left:10px;text-decoration:underline;font-weight:bold;" >
                    Card ID
                    </td>
                    <td style="padding-left:20px;">
                        <asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox>
                    </td>
                    <td style="padding-left:20px;text-decoration:underline;font-weight:bold;">
                        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Button1_Click" OnClick="Button1_Click" />
                    </td>

                </tr>
        </table><br/>       


    <HeaderTemplate>
            <table>
                <tr>
                    <td style="padding-left:10px;text-decoration:underline;font-weight:bold;" >
                    Name 
                    </td>
                    <td style="padding-left:10px;text-decoration:underline;font-weight:bold;" >
                    Surname
                    </td>
                    <td style="padding-left:20px;text-decoration:underline;font-weight:bold;">
                    Type
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
            <td style="padding-left:10px;" valign="top"> <%# XPath("name")%>
            </td>
            <td style="padding-left:20px;" valign="top"> <%# XPath("surname") %>
            </td>
            <td style="padding-left:20px;" valign="top"> <%# XPath("type")%>
            </tr>
         </ItemTemplate>

       <FooterTemplate>
           <tr>
            <td colspan ="3"> 
            </td>
           </tr>
            </table>
       </FooterTemplate><br/>



 </asp:Content>

这是aspx.cs:

 <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <h1><p>Sala fitness</p></h1>

    <asp:XmlDataSource ID="SalaDataSource" runat="server" DataFile="~/App_Data/clienti.xml" XPath="/clienti/persoana" ></asp:XmlDataSource>
    <asp:DataList ID="dlPerson" runat="server" DataSourceID="SalaDataSource"> </asp:DataList>

    <h2>Verify member</h2><br/>

         <table>
                <tr>
                    <td style="padding-left:10px;text-decoration:underline;font-weight:bold;" >
                    Card ID
                    </td>
                    <td style="padding-left:20px;">
                        <asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox>
                    </td>
                    <td style="padding-left:20px;text-decoration:underline;font-weight:bold;">
                        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Button1_Click" OnClick="Button1_Click" />
                    </td>

                </tr>
        </table><br/>       


    <HeaderTemplate>
            <table>
                <tr>
                    <td style="padding-left:10px;text-decoration:underline;font-weight:bold;" >
                    Name 
                    </td>
                    <td style="padding-left:10px;text-decoration:underline;font-weight:bold;" >
                    Surname
                    </td>
                    <td style="padding-left:20px;text-decoration:underline;font-weight:bold;">
                    Type
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
            <td style="padding-left:10px;" valign="top"> <%# XPath("name")%>
            </td>
            <td style="padding-left:20px;" valign="top"> <%# XPath("surname") %>
            </td>
            <td style="padding-left:20px;" valign="top"> <%# XPath("type")%>
            </tr>
         </ItemTemplate>

       <FooterTemplate>
           <tr>
            <td colspan ="3"> 
            </td>
           </tr>
            </table>
       </FooterTemplate><br/>

    <asp:HyperLink ID="HyperLink1" runat="server">Inregistreaza client nou</asp:HyperLink>

 </asp:Content>

1 个答案:

答案 0 :(得分:0)

以下是在C#中使用XMLDataSource的方法:

  • 创建POCO以映射XML文档
  • 使用XmlSerializer反序列化您的XML(这样您将获得客户列表)

现在做

var clientObject=  clientList.where(t=>t.id==textbox1.Text).FirstOrDefult();

现在使用此对象的属性绑定数据表。