简体   繁体   English

使用C#根据两个属性将XML转换为DataTable

[英]Convert XML to DataTable according to two attributes using C#

I have an XML file and I want to make a DataTable and bind it to a GridView , but the data must be taken from the XML file according to attributes, not the tag. 我有一个XML文件,我想制作一个DataTable并将其绑定到GridView ,但是必须根据属性而不是标签从XML文件中获取数据。 The first attribute is "m" and it represents the row index. 第一个属性是“ m”,它表示行索引。 The other attribute is "s" and it represents the column index. 另一个属性是“ s”,它表示列索引。 If the attribute isn't mentioned in some tags, the default value for it is "1" 如果某些标记中未提及该属性,则其默认值为“ 1”

The output must be shown in GridView like this form 输出必须以这种形式显示在GridView

 <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>

the xml file is xml文件是

<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>

Try this 尝试这个

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string xml =
            "<Root>" +
               "<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>" +
               "</Root>";

            XDocument doc = XDocument.Parse(xml);

            var results = doc.Descendants().Where(m => (m.Name.LocalName != "row") && (m.Name.LocalName != "Root"))
                .GroupBy(x => x.Name.LocalName)
                .Select(y => new
                {
                    name = y.Where(z => z.Attribute("m") == null).Select(a => a.Value).FirstOrDefault(),
                    months = y.Where(z => z.Attribute("m") != null).GroupBy(z => z.Attribute("m")).Select(a => new
                    {
                        month = (int)a.Key,
                        section = a.Select(b => new
                        {
                            s = b.Attribute("s") == null ? null : (int?)b.Attribute("s"),
                            value = b.Value
                        }).FirstOrDefault()
                    }).ToList()
                }).ToList();

            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Month", typeof(int));
            dt.Columns.Add("Section", typeof(int));
            dt.Columns.Add("Value", typeof(string));

            foreach (var name in results.AsEnumerable())
            {
                foreach (var month in name.months.AsEnumerable())
                {

                    if (month.section.s == null)
                    {
                        DataRow newRow = dt.Rows.Add(new object[] { name.name, month.month, 1, month.section.value });
                    }
                    else
                    {
                        DataRow newRow = dt.Rows.Add(new object[] { name.name, month.month, month.section.s, month.section.value });
                    }

                }
            }
            dataGridView1.DataSource = dt;
        }
    }
}

转换XML <!--CDATA to DataTable C#</div--><div id="text_translate"><p> 我有 Webservice SOAP 方法,它将 Json 作为输入参数。</p><pre> //For sample string jsonString = "[{"Year":"2020","UserId":"1","Comp":"20","DeptId":"32","CategoryId":"53","ItemId":"0"}]"; string[] processString = budget.BudgetTagNet(jsonString);</pre><p> 现在“budget.BudgetTagNet(jsonString)”返回前面提到的字符串数组,并且 SOAP 响应显示以下值: <a href="https://i.stack.imgur.com/ms2hp.png" rel="nofollow noreferrer">SOAP 响应结果</a></p><p>现在我想将返回的预算数组转换为 DataTable 以进一步用于我的项目,但我做不到。 实现这一目标的最佳方法是什么。 我尝试过的方法如下所述。</p><pre> string jsonString = CommonEnum.DataTableToJSON(Table); err.Message = "Json String is: " + jsonString; err.Insert(); string[] processString = budget.BudgetTagNet(jsonString); err.Message = "Budget service data is: " + processString[1]; err.Insert(); var XMLAsString = CommonEnum.SerializeXML(processString[1]); err.Message = "Converted XML To String data is: " + JsonConvert.SerializeObject(XMLAsString); err.Insert(); dt = (DataTable) JsonConvert.DeserializeObject(XMLAsString, (typeof(DataTable))); err.Message = "DataTable data: " + JsonConvert.SerializeObject(dt); err.Insert();</pre><p> 最后插入数据库中的数据,如上述方法是:<a href="https://i.stack.imgur.com/zSY2Q.png" rel="nofollow noreferrer">插入的数据库值</a></p></div> - Convert XML <!CDATA to DataTable C#

暂无
暂无

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

相关问题 C#中有两个属性的Xml - Xml with two attributes in C# 使用 c# 将 xml(混合节点和属性)转换为 csv 文件 - Convert xml (mixed nodes and attributes) to csv file using c# 转换XML <!--CDATA to DataTable C#</div--><div id="text_translate"><p> 我有 Webservice SOAP 方法,它将 Json 作为输入参数。</p><pre> //For sample string jsonString = "[{"Year":"2020","UserId":"1","Comp":"20","DeptId":"32","CategoryId":"53","ItemId":"0"}]"; string[] processString = budget.BudgetTagNet(jsonString);</pre><p> 现在“budget.BudgetTagNet(jsonString)”返回前面提到的字符串数组,并且 SOAP 响应显示以下值: <a href="https://i.stack.imgur.com/ms2hp.png" rel="nofollow noreferrer">SOAP 响应结果</a></p><p>现在我想将返回的预算数组转换为 DataTable 以进一步用于我的项目,但我做不到。 实现这一目标的最佳方法是什么。 我尝试过的方法如下所述。</p><pre> string jsonString = CommonEnum.DataTableToJSON(Table); err.Message = "Json String is: " + jsonString; err.Insert(); string[] processString = budget.BudgetTagNet(jsonString); err.Message = "Budget service data is: " + processString[1]; err.Insert(); var XMLAsString = CommonEnum.SerializeXML(processString[1]); err.Message = "Converted XML To String data is: " + JsonConvert.SerializeObject(XMLAsString); err.Insert(); dt = (DataTable) JsonConvert.DeserializeObject(XMLAsString, (typeof(DataTable))); err.Message = "DataTable data: " + JsonConvert.SerializeObject(dt); err.Insert();</pre><p> 最后插入数据库中的数据,如上述方法是:<a href="https://i.stack.imgur.com/zSY2Q.png" rel="nofollow noreferrer">插入的数据库值</a></p></div> - Convert XML <!CDATA to DataTable C# 如何在C#中将xml转换为DataTable - how to convert xml to DataTable in C# 将XML文件转换为数据表C# - Convert XML file to datatable C# 在C#中将DataTable转换为XML层次结构 - Convert DataTable into XML hierarchy in C# C#将xml属性转换为元素 - C# to convert xml attributes to elements 使用C#获取xml的属性和值 - getting attributes and values of xml using C# 使用 C# 将 XML 节点转换为属性 - Converting XML nodes into attributes using C# 使用C#删除XML中的属性数组 - Removing an Array of attributes in XML using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM