位置 :  首页 / 博客 / 学无止境

ASP.NET Repeater遁环绑定XML

发布人:南昌开优网络 发布时间:2018/12/20 访问量:1445


XML示例:

<?xml version="1.0" encoding="utf-8"?>
<Response>
  <ResultContent>成功</ResultContent>
  <ResultCode>0</ResultCode>
  <SFZH>aaaaaaaa</SFZH>
  <TJID>体检ID</TJID>
  <XM>姓名</XM>
  <XB>性别</XB>
  <CSNY>出生</CSNY>
  <ZHXMMC></ZHXMMC>
  <ExamItemList>
    <ExamItem>
      <KSBM>YB</KSBM>
      <KSMC>一般检查</KSMC>
      <SFXMDM>176</SFXMDM>
      <SFXMMC>一般检查(身高、体重、血压)</SFXMMC>
      <IMAGE>0</IMAGE>
      <Dcotor>医生</Dcotor>
      <CheckDate>2018/10/23 10:05:08</CheckDate>
      <MX>
        <XXDM>010117</XXDM>
        <XXMC>其他</XXMC>
        <ItemResult>未见明显异常</ItemResult>
        <ItemUnit>
        </ItemUnit>
        <DefValue>
        </DefValue>
        <Flag>
          <AA>测试3层A</AA>
          <BB>测试3层B</BB>
        </Flag>
      </MX>
      <MX>
        <XXDM>010109</XXDM>
        <XXMC>舒张压</XXMC>
        <ItemResult>65</ItemResult>
        <ItemUnit>mmHg</ItemUnit>
        <DefValue>
        </DefValue>
        <Flag>
        </Flag>
      </MX>
      <MX>
        <XXDM>010105</XXDM>
        <XXMC>体重指数</XXMC>
        <ItemResult>20.03</ItemResult>
        <ItemUnit>
        </ItemUnit>
        <DefValue>
        </DefValue>
        <Flag>
        </Flag>
      </MX>    
    </ExamItem>
    <ExamItem>
      <KSBM>HY</KSBM>
      <KSMC>化验室</KSMC>
      <SFXMDM>024</SFXMDM>
      <SFXMMC>尿液分析</SFXMMC>
      <IMAGE>0</IMAGE>
      <Dcotor>医生</Dcotor>
      <CheckDate>2018/10/23 0:00:00</CheckDate>
      <MX>
        <XXDM>030206</XXDM>
        <XXMC>隐血</XXMC>
        <ItemResult>阴性</ItemResult>
        <ItemUnit>
        </ItemUnit>
        <DefValue>阴性</DefValue>
        <Flag>
        </Flag>
      </MX>
      <MX>
        <XXDM>030205</XXDM>
        <XXMC>比重SG</XXMC>
        <ItemResult>1.025</ItemResult>
        <ItemUnit>
        </ItemUnit>
        <DefValue>1.003-1.030</DefValue>
        <Flag>
        </Flag>
      </MX>
      <MX>
        <XXDM>030204</XXDM>
        <XXMC>维生素C</XXMC>
        <ItemResult>阴性</ItemResult>
        <ItemUnit>
        </ItemUnit>
        <DefValue>阴性</DefValue>
        <Flag>
        </Flag>
      </MX>     
    </ExamItem>
  </ExamItemList>
  <JCJG>
    1. 一般检查结论:<br /><br />内容内容内容内容内容内容<br /><br />2. 内容内容内容内容内容内容内容内容内容内容:<br /><br />
  </JCJG>
  <ZJJL>
    【1. 膀胱显示不清】<br /><br />【2. 红细胞计数RBC偏低】<br />
  </ZJJL>
  <JKJY>
    【1. 内容内容】<br />内容内容内容内容,<br /><br />【2. 内容内容内容内容偏低】<br />内容内容内容内容内容内容内容内容<br /><br /><br />- 谢谢合作      祝您健康!<br />
  </JKJY>
  <ZJDcotor>内容内容内容内容</ZJDcotor>
  <ZJDate>2018-11-05 08:42:06</ZJDate>
</Response>



前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="XiaobinManage.Web.test" %>
<%@ Import Namespace="System.Xml" %><!--引用System.Xml类-->
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>
            <asp:Literal ID="lit_SFZH" runat="server"></asp:Literal></h2><!--通过后台显示文字-->        
        <asp:Repeater ID="rep1" runat="server" OnItemDataBound="rep1_ItemDataBound">  <!--rep第1次绑定 rep1_ItemDataBound -->
            <ItemTemplate>
                <h3><%#((XmlNode)Container.DataItem).SelectSingleNode("KSMC").InnerText%></h3>   <!--前台显示文本-->
                <table border="1">
                    <asp:Repeater ID="rep2" runat="server" OnItemDataBound="rep2_ItemDataBound"> <!--rep第2次绑定 rep2_ItemDataBound -->
                        <ItemTemplate>
                            <tr>
                                <td><%#XPath("XXDM")%></td>  <!-- XPath类似于Eval() 前台绑定显示该节点-->
                                <td><%#XPath("ItemResult")%></td> <!--前台绑定显示该节点-->
                                <td>
                                    <table border="1">
                                        <asp:Repeater ID="rep3" runat="server"> <!--rep第3次绑定 -->
                                            <ItemTemplate>
                                                <tr>
                                                    <td><%#XPath("AA")%></td> <!--前台绑定显示该节点-->
                                                    <td><%#XPath("BB")%></td> <!--前台绑定显示该节点-->
                                                </tr>
                                            </ItemTemplate>
                                        </asp:Repeater>
                                    </table>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>


后台CS:

  public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); //引用XmlDocument类
                xml.Load(Server.MapPath("test.xml")); //打开文件

                lit_SFZH.Text = xml.DocumentElement.SelectSingleNode("SFZH").InnerText; //显示前台标签文本 InnerText为文本,InnerXml为html格式

                rep1.DataSource = xml.DocumentElement.SelectSingleNode("ExamItemList").ChildNodes; //获取SelectSingleNode指定节点的第一个下的所有节点
                rep1.DataBind();
            }
        }

        protected void rep1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Repeater rep2 = e.Item.FindControl("rep2") as Repeater;

                System.Xml.XPath.XPathNavigator nav = ((System.Xml.XPath.IXPathNavigable)e.Item.DataItem).CreateNavigator(); //绑定e.Item的XPath对象
                System.Xml.XPath.XPathNodeIterator it = nav.Select("MX"); //选择Select指定节点的第一个下的所有节点

                rep2.DataSource = it;
                rep2.DataBind();
            }
        }

        protected void rep2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Repeater rep3 = e.Item.FindControl("rep3") as Repeater;

                IEnumerable em = XPathBinder.Select(e.Item.DataItem, "Flag") as IEnumerable; //获取Select指定节点的第一个下的所有节点
                em.GetEnumerator();

                rep3.DataSource = em;
                rep3.DataBind();
            }
        }

    }