根据两个属性从xml文件构建数据表 [英] Build datatable from xml file according to two attributes

查看:65
本文介绍了根据两个属性从xml文件构建数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,我想制作一个数据表并将其绑定在网格视图中,但数据必须根据属性而不是标签从XML文件中获取

第一个属性是m表示行索引,另一个属性为s,它表示列索引

如果某些标记中未提及该属性,则默认值为1



输出必须在gridview中显示,如此表格



I have an XML file and i want to make a data table and bind it in grid view , but the data must taken from the XML file according to attributes not the tag
the first attribute is "m" and it represents the row index, the other attribute is "s" and it represents the column index
if the attribute doesn't mentioned in some tags the default value for it is "1"

the outputs must shown in gridview like this form

<asp:GridView id="grdBank" runat="server" AutoGenerateColumns="False" CellPadding="0" >
                               <Columns>

                                   <asp:BoundField HeaderText="Repay date" DataField="repay">
                                       <HeaderStyle Font-Bold="True"></HeaderStyle>
                                   </asp:BoundField>

                                      <asp:BoundField HeaderText="Interest" DataField="interest">
                                       <HeaderStyle Font-Bold="True"></HeaderStyle>
                                   </asp:BoundField>

                                <asp:BoundField HeaderText="Penalty interest" DataField="penalty_interest">
                                       <HeaderStyle Font-Bold="True"></HeaderStyle>
                                   </asp:BoundField>

                               <asp:BoundField HeaderText="Penalty spread" DataField="penalty_spread">
                                       <HeaderStyle Font-Bold="True"></HeaderStyle>
                                   </asp:BoundField>

                               <asp:BoundField HeaderText="Others (CE+CS)" DataField="others">
                                       <HeaderStyle Font-Bold="True"></HeaderStyle>
                                   </asp:BoundField>

                               </Columns>
                             </asp:GridView>







XML文件是:






XML File is:

<row>
<c24>20160201</c24>
<c24 m="2">20160131</c24>
<c24 m="3">20160101</c24>
<c24 m="4">20151231</c24>
<c24 m="5">20151201</c24>
<c24 m="6">20151130</c24>
<c24 m="7">20151102</c24>
</row>



<row>
<c28>IN</c28>
<c28 m="1" s="2">PE</c28>
<c28 m="1" s="3">PS</c28>

<c28 m="2">PR</c28>
<c28 m="2" s="2">PE</c28>
<c28 m="2" s="3">PS</c28>

<c28 m="3">IN</c28>
<c28 m="3" s="2">PE</c28>
<c28 m="3" s="3">PS</c28>
<c28 m="3" s="4">CE</c28>
<c28 m="3" s="5">CS</c28>

<c28 m="4">PR</c28>
<c28 m="4" s="2">PE</c28>
<c28 m="4" s="3">PS</c28>
<c28 m="4" s="4">CE</c28>
<c28 m="4" s="5">CS</c28>

<c28 m="5">IN</c28>
<c28 m="5" s="2">PE</c28>
<c28 m="5" s="3">PS</c28>
<c28 m="5" s="4">CE</c28>
<c28 m="5" s="5">CS</c28>

<c28 m="6">PR</c28>
<c28 m="6" s="2">PE</c28>
<c28 m="6" s="3">PS</c28>
<c28 m="6" s="4">CE</c28>
<c28 m="6" s="5">CS</c28>

<c28 m="7">PR</c28>
<c28 m="7" s="2">PE</c28>
<c28 m="7" s="3">PS</c28>
<c28 m="7" s="4">CE</c28>
<c28 m="7" s="5">CS</c28>
</row>




<row>
<c29>1334.564</c29>
<c29 m="1" s="2">9.509</c29>
<c29 m="1" s="3">3.003</c29>

<c29 m="2">3900</c29>
<c29 m="2" s="2">28.817</c29>
<c29 m="2" s="3">9.1</c29>

<c29 m="3">1366.468</c29>
<c29 m="3" s="2">10.097</c29>
<c29 m="3" s="3">3.189</c29>
<c29 m="3" s="4">10.818</c29>
<c29 m="3" s="5">3.416</c29>

<c29 m="4">3900</c29>
<c29 m="4" s="2">28.817</c29>
<c29 m="4" s="3">9.1</c29>
<c29 m="4" s="4">31.904</c29>
<c29 m="4" s="5">10.075</c29>

<c29 m="5">1353.571</c29>
<c29 m="5" s="2">10.001</c29>
<c29 m="5" s="3">3.159</c29>
<c29 m="5" s="4">21.789</c29>
<c29 m="5" s="5">6.881</c29>

<c29 m="6">3900</c29>
<c29 m="6" s="2">28.817</c29>
<c29 m="6" s="3">9.1</c29>
<c29 m="6" s="4">63.808</c29>
<c29 m="6" s="5">20.15</c29>

<c29 m="7">1290.211</c29>
<c29 m="7" s="2">9.533</c29>
<c29 m="7" s="3">3.011</c29>
<c29 m="7" s="4">30.641</c29>
<c29 m="7" s="5">9.678</c29>
</row>





What I have tried:



I tried this code



What I have tried:

I tried this code

XmlTextReader reader = null;
          DataTable Items = new DataTable();
                     Items.Columns.Add("repay");
                     Items.Columns.Add("interest");
                     Items.Columns.Add("penalty_interest");
                     Items.Columns.Add("penalty_spread");
                     Items.Columns.Add("others");
                     DataRow dr=Items.NewRow();
         try
 {

    //Load the reader with the XML file.
    reader = new XmlTextReader("attrs.xml");

    //Read the m attribute.
    reader.MoveToContent();

              for (int i = 0; i < reader.AttributeCount; i++) {
    string m = reader.GetAttribute("m");

          dr["repay"]= Convert.ToString(new Random().Next(5, 55));
         //dt.Rows.Add(row1);

    //dr[AttName] = reader.Value;
              }


  }



  finally
  {
     if (reader != null)
       reader.Close();
   }

推荐答案

Unless the XML document is huge, I suggest using an XmlDocument (my preference) or XDocument rather than an XmlTextReader. XmlNode.SelectNodes Method (String) (System.Xml)[^]

Then you can simply use XPath to access the contents (including attributes) by name. XML and XPath[^]
Unless the XML document is huge, I suggest using an XmlDocument (my preference) or XDocument rather than an XmlTextReader. XmlNode.SelectNodes Method (String) (System.Xml)[^]
Then you can simply use XPath to access the contents (including attributes) by name. XML and XPath[^]


这篇关于根据两个属性从xml文件构建数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆