C#读取XML文件

1、一般处理程序查询数据返回xml类型的字符串:

       public string ProjectData()
        {
            string connectionStrings = BookStrap.SqlHelp.constr;
            string sql = "select ID,ProjectName,Lng,Lat,jsdw,extStaus  from LbtProjectMap";
            DataTable dt = BookStrap.SqlHelp.ExecuteDataset(connectionStrings, sql, CommandType.Text, null).Tables[0];
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            xml += "<rows>";
            foreach (DataRow dr in dt.Rows)
            {
                xml += "<row id=\"" + dr["ID"] + "\">";
                xml += "<cell>" + dr["ID"] + "</cell>";
                xml += "<cell>" + dr["ProjectName"] + "</cell>";
                xml += "<cell>" + dr["Lng"] + "</cell>";
                xml += "<cell>" + dr["Lat"] + "</cell>";
                xml += "<cell>" + dr["jsdw"] + "</cell>";
                xml += "<cell>" + dr["extStaus"] + "</cell>";
                xml += "</row>";
            }
            xml += "</rows>";
            return xml;
        }

2、Jquery点击按钮读取xml类型的string字符串,绑定到table上:

   $(function () {
            $("#Button1").click(function () {             
                $.ajax({
                    url: 'Handler2.ashx',
                    type: 'get',
                    //data: "",
                    dataType: 'text',
                    success: function (data) {
                        var htmls = "<table width=\"1200\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
                        htmls += "<thead>";
                        htmls += "<tr>";
                        htmls += "<th>ID</th>";
                        htmls += "<th>工程名称</th>";
                        htmls += "<th>经度</th>";
                        htmls += "<th>纬度</th>";
                        htmls += "<th>建设单位</th>";
                        htmls += "<th>实施状态</th>";
                        htmls += "</tr></thead>";
                        console.log(typeof (data));//打印string类型
                        var $xmlDoc = $($.parseXML(data));
                        var $rows = $xmlDoc.find("row");

                        for (var i = 0; i < $rows.length; i++) {
                            console.log($rows[i]);//读取row
                            
                            console.log($rows[i].innerHTML);//读取row里面的所有cell
                            htmls += "<tr>";
                            //console.log($rows[i].textContent);//

                            var childl = $rows[i].children;
                            console.log($rows[i].children);//打印出HTMLCollection对象
                            for (var j = 0; j < childl.length; j++) {
                                console.log(childl[j].innerHTML);
                                htmls += "<td>"+childl[j].innerHTML+"</td>";
                            }                    
                            htmls += "</tr>";
                        }
                        htmls += "</table>";
                      
                        $("body").append(htmls);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert("加载失败");
                    }

                });
                 
            })
        })

html代码:

<body>
    <input id="Button1" type="button" value="XML文件" />
 
</body>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值