StringTemplate.NET实例6

时间:2009-11-07 阅读: 佚名
先定义一个简单的类 class User { string m_name = string.Empty; string m_age = string.Empty; public string Name { set { m_name = value; } get { return m_name; } } public st

先定义一个简单的类

    class User
    {
        string m_name = string.Empty;
        string m_age = string.Empty;
        public string Name
        {
            set { m_name = value; }
            get { return m_name; }
        }
        public string Age
        {
            set { m_age = value; }
            get { return m_age; }
        }
    }

---------------------------------------------------

            User us1 = new User();
            us1.Name = "张三";
            us1.Age = "23";
            User us2 = new User();
            us2.Name = "李四";
            us2.Age = "24";
            List<User> uss = new List<User>();
            uss.Add(us1);
            uss.Add(us2);
            StringTemplate st = new StringTemplate(
              "<table>\r\n<tr><th>姓名</th><th>年龄</th></tr>\r\n$Item:{<tr<td>$it.name$</td><td>$it.age$</td></tr>};separator=\"\r\n\"$\r\n</table>");
            st.SetAttribute("Item", uss);

结果如下:

<table>
<tr><th>姓名</th><th>年龄</th></tr>
<tr<td>张三</td><td>23</td></tr>
<tr<td>李四</td><td>24</td></tr>
</table>

[责任编辑:jumbot]

[发表评论] [收藏本页]