简体   繁体   English

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

[英]XML node to table if condition is true (C# , ASP.net)

I am just beginning C# and ASP.Net and I need help populating a table. 我刚刚开始使用C#和ASP.Net,并且需要填充表的帮助。 This is how the xml looks like: 这是xml的样子:

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

I want to enter the id in a text field, press a submit button and if the id matches with one of the ids from the xml file to populate a table with name. 我想在文本字段中输入ID,然后按一下提交按钮,如果ID与xml文件中的ID之一匹配,则用名称填充表。 So far if I press the button nothing happens. 到目前为止,如果我按下按钮,则什么也不会发生。

This is my aspx so far (I believe "</asp:DataList>" should be at the end, but if I do so it will give an error): 到目前为止,这是我的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>

and this is the aspx.cs: 这是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>

Here is what should be your approach to use XMLDataSource in C#: 这是在C#中使用XMLDataSource的方法应该是什么:

  • Create POCOs to map your XML document 创建POCO以映射您的XML文档
  • Deserialize your XML using XmlSerializer (This way you will get the list of Clients) 使用XmlSerializer反序列化XML(通过这种方式,您将获得客户端列表)

Now do 现在做

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

Now use this object's properties to bind a datatable. 现在,使用该对象的属性来绑定数据表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM