将XML提供给datagridview C# [英] Feed XML to a datagridview C#

查看:67
本文介绍了将XML提供给datagridview C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将以下XML提供给数据网格视图

< root type =object> 
< gstin type =string> 24AB3584G1ZL< / gstin>
< fp type =string> 082017< / fp>
< b2b type =array>
< item type =object>
< ctin type =string> 24AC7232H1ZR< / ctin>
< cfs type =string> Y< / cfs>
< cname type =null>< / cname>
< inv type =array>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 1476.75< / samt>
< rt type =number> 5< / rt>
< txval type =number> 59070< / txval>
< camt type =number> 1476.75< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 62024< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 12-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 217 / G< / inum>
< chksum type =string> fb9401722f9f9f46cfa8ddf70647ebd47a3ca1094f4c6a17e874aea79296dcb8< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 1408.38< / samt>
< rt type =number> 5< / rt>
< txval type =number> 56335< / txval>
< camt type =number> 1408.38< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 59152< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 12-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 218 / G< / inum>
< chksum type =string> d06e30bc81f3ea8f83b735104967f878119a635dd898b33e954f41d87ebacc8c< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 1393.5< / samt>
< rt type =number> 5< / rt>
< txval type =number> 55740< / txval>
< camt type =number> 1393.5< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 58527< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 16-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 222 / G< / inum>
< chksum type =string> 1a5e45053bf0665455557ea3df6b0d4d170c595b646ed54d52ef561bb66bdf77< / chksum>
< / item>
< / inv>
< / item>
< item type =object>
< ctin type =string> 24AA3022L1ZJ< / ctin>
< cfs type =string> Y< / cfs>
< cname type =null>< / cname>
< inv type =array>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 1< / num>
< itm_det type =object>
< csamt type =number> 0< / csamt>
< samt type =number> 1240.7< / samt>
< rt type =number> 5< / rt>
< txval type =number> 49628< / txval>
< camt type =number> 1240.7< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 52109< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 27-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 13/119< / inum>
< chksum type =string> 5951d10d389e1b6d9f6b88c536b5fc5f3676b9dd15d977a3fbe741407bd3796f< / chksum>
< / item>
< / inv>
< / item>
< item type =object>
< ctin type =string> 24AI654J1ZX< / ctin>
< cfs type =string> Y< / cfs>
< cname type =null>< / cname>
< inv type =array>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 1< / num>
< itm_det type =object>
< csamt type =number> 0< / csamt>
< samt type =number> 1263.83< / samt>
< rt type =number> 5< / rt>
< txval type =number> 50553< / txval>
< camt type =number> 1263.83< / camt>
< iamt type =number> 0< / iamt>
< / itm_det>
< / item>
< / itms>
< val type =number> 53080.65< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 23-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 149< / inum>
< chksum type =string> ed7e5e187ea6349faff81f7e2eb2910a87ec4df0d70f388aa1ba4b46312032ba< / chksum>
< / item>
< / inv>
< / item>
< item type =object>
< ctin type =string> 24A6274F1ZA< / ctin>
< cfs type =string> Y< / cfs>
< cname type =null>< / cname>
< inv type =array>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 1< / num>
< itm_det type =object>
< csamt type =number> 0< / csamt>
< samt type =number> 748.65< / samt>
< rt type =number> 5< / rt>
< txval type =number> 29946< / txval>
< camt type =number> 748.65< / camt>
< iamt type =number> 0< / iamt>
< / itm_det>
< / item>
< / itms>
< val type =number> 31443< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 01-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 594< / inum>
< chksum type =string> c3c6c3b9651ded60d8cd58d5659aa99e5f08a50b04e261a647fdd2e55958c03c< / chksum>
< / item>
< / inv>
< / item>
< item type =object>
< ctin type =string> 24AA064Q1ZW< / ctin>
< cfs type =string> Y< / cfs>
< cname type =null>< / cname>
< inv type =array>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 649.45< / samt>
< rt type =number> 5< / rt>
< txval type =number> 25978< / txval>
< camt type =number> 649.45< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 27277< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 04-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 5685 / J< / inum>
< chksum type =string> e4809d5e25b7df166fe140851c6d74244807c8de759bfd251cf9f2f06a4fabf5< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 136.8< / samt>
< rt type =number> 5< / rt>
< txval type =number> 5472< / txval>
< camt type =number> 136.8< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 5746< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 05-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 5721 / J< / inum>
< chksum type =string> 1244f4d49e7a3e50f42bb9b0f87ebd961d4a7d0900c84d5d1feff9788c27b151< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 130.97< / samt>
< rt type =number> 5< / rt>
< txval type =number> 5239< / txval>
< camt type =number> 130.97< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 5501< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 05-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 5753 / J< / inum>
< chksum type =string> 1eb028428be75cfdc790a0de2f7a9197099166617b46e7a4c9e4a31de17b194f< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 415.8< / samt>
< rt type =number> 5< / rt>
< txval type =number> 16632< / txval>
< camt type =number> 415.8< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 17464< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 08-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 5826 / J< / inum>
< chksum type =string> c174a888d8f2ba95c78354ebefcb41d75b37902daf7ca43c2f2b2e022e501dde< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 272.8< / samt>
< rt type =number> 5< / rt>
< txval type =number> 10912< / txval>
< camt type =number> 272.8< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 11458< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 09-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 5866 / J< / inum>
< chksum type =string> b1fce912197d109cf4bf19f7b585bbffabe7bcd53772369ce2a2935a0d4162bc< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 429.35< / samt>
< rt type =number> 5< / rt>
< txval type =number> 17174< / txval>
< camt type =number> 429.35< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 18033< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 21-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 6437 / J< / inum>
< chksum type =string> 3577ce8549a7e8853eb00b0737b2f94352abbbd70f6a12946dc803d4a245895a< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 288.7< / samt>
< rt type =number> 5< / rt>
< txval type =number> 11548< / txval>
< camt type =number> 288.7< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 12125< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 22-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 6470 / J< / inum>
< chksum type =string> ef6104e50fceec2e841aaccd2814e522b2c06803fddde53cf2a343e7a551eb32< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 1515.53< / samt>
< rt type =number> 5< / rt>
< txval type =number> 60621< / txval>
< camt type =number> 1515.53< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 63652< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 26-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 6796 / J< / inum>
< chksum type =string> 8b666418ab813d2030d882e0f18071da91d83adfbe6d7b0ac557d70aad0877ca< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 1360.13< / samt>
< rt type =number> 5< / rt>
< txval type =number> 54405< / txval>
< camt type =number> 1360.13< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 57125< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 28-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 6897 / J< / inum>
< chksum type =string> c8fb4ae410a3283c5221e9203167dc1687791a87179850c51a0f15346b317541< / chksum>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 342.73< / samt>
< rt type =number> 5< / rt>
< txval type =number> 13709< / txval>
< camt type =number> 342.73< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 14394< / val>
< inv_typ type =string> R< / inv_typ>
< pos type =string> 24< / pos>
< idt type =string> 29-08-2017< / idt>
< rchrg type =string> N< / rchrg>
< inum type =string> 6931 / J< / inum>
< chksum type =string> 07ff8bcde46c86e427a2fa850c58d643f2adc32182ec674091c94f2419a787dd< / chksum>
< / item>
< / inv>
< / item>
< / b2b>
< cdn type =array>
< item type =object>
< cfs type =string> Y< / cfs>
< ctin type =string> 24A0064Q1ZW< / ctin>
< cname type =null>< / cname>
< nt type =array>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 59.23< / samt>
< rt type =number> 5< / rt>
< txval type =number> 2369< / txval>
< camt type =number> 59.23< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 2487< / val>
< idt type =string> 26-08-2017< / idt>
< ntty type =string> C< / ntty>
< nt_num type =string> 65< / nt_num>
< inum type =string> 6796 / J< / inum>
< rsn type =string> 02-Post Sale折扣< / rsn>
< nt_dt type =string> 31-08-2017< / nt_dt>
< chksum type =string> ec636db911bbcf11b1aa200c91fe72ee4850e74886530247dc7f54353b912c93< / chksum>
< p_gst type =string> N< / p_gst>
< / item>
< item type =object>
< itms type =array>
< item type =object>
< num type =number> 500< / num>
< itm_det type =object>
< samt type =number> 353.98< / samt>
< rt type =number> 5< / rt>
< txval type =number> 14159< / txval>
< camt type =number> 353.98< / camt>
< / itm_det>
< / item>
< / itms>
< val type =number> 14867< / val>
< idt type =string> 28-08-2017< / idt>
< ntty type =string> C< / ntty>
< nt_num type =string> 74 / C< / nt_num>
< inum type =string> 6897 / J< / inum>
< rsn type =string> 02-Post Sale折扣< / rsn>
< nt_dt type =string> 31-08-2017< / nt_dt>
< chksum type =string> 4cf679fd864f55bcc9c4eafb039deddeb6df8ed97b495f4023548946c884b39e< / chksum>
< p_gst type =string> N< / p_gst>
< / item>
< / nt>
< / item>
< / cdn>
< / root>





我尝试过:



我尝试过DataSet.ReadXML,但由于有一个共同的父子表,所以它没有用。

 





需要这种格式

RequiredOutput.csv - Google云端硬盘 [ ^ ]

解决方案

如果您的XML格式不正确,则需要使用 XmlReader 逐行阅读,请参见此处的示例: [ Dotnetperls ]

痛苦而笨拙,但这就是我害怕的方式: )


我很费劲建议使用 xsd.exe工具 [ ^ ],这有助于从xml创建类。然后你就可以使用 XmlSerialization [ ^ ]转换xml数据到对象,反之亦然。



注意:xsd工具在Microsoft SDK文件夹中可用。请参阅: xml - 在哪里可以找到xsd Windows 8上的Visual Studio 2013中的.exe - Stack Overflow [ ^ ]



详情请见:

如何:使用XML架构用于生成类和XML模式文档的定义工具| Microsoft Docs [ ^ ]

使用xsd.exe自动生成实体类,用于XML序列化和反序列化 - Yogesh Joshi的博客 [ ^ ]

XML序列化 - 提示&技巧 [ ^ ]

XML序列化和反序列化:第1部分 [ ^ ]

XML序列化和反序列化:第2部分 [ ^ ]

我终于破解了它,诀窍是使用递归,这里是更新的动态版本:

使用System.Windows.Forms; 
使用System.Collections.Generic;
使用System.Diagnostics;
使用System.Xml.Linq;
使用System;

命名空间DataGridViewXml
{
public partial class Form2:Form
{
private string xmlpath = @test.xml;
public Dictionary< string,int> dgvHeaders;
public BindingSource dgvSource;
public object [] dgvRow;

public Form2()
{
InitializeComponent();
this.Run();
}

public void Run()
{
dgvHeaders = new Dictionary< string,int>();
XElement xelement = XElement.Load(xmlpath);

GetHeaders(xelement);

foreach(dgvHeaders中的var标头)
{
Debug.Print(header.Key.PadRight(20)+ header.Value);
dataGridView1.Columns.Add(header.Key,header.Key);
}

dgvRow =新对象[dgvHeaders.Count];
RecurseElements(xelement);
}

private void GetHeaders(XElement xelement)
{
foreach(xelement.Elements()中的var objx)
{
if (objx.HasElements)
{
GetHeaders(objx);
}
else
{
string header = objx.Name.ToString();

if(!dgvHeaders.ContainsKey(header))
{
//只有在字典中没有标题时才添加标题。
dgvHeaders.Add(header,dgvHeaders.Count);
}
}
}
}

private void RecurseElements(XElement xelement)
{
foreach(xelement中的var objx) .Elements())
{
if(objx.HasElements)
{
RecurseElements(objx);
}
else
{
string name = objx.Name.ToString();
Debug.Print(name +=+ objx.Value.ToString());

int columnPos = 0;
dgvHeaders.TryGetValue(name,out columnPos);

//构造一行。
if(columnPos> = 0)
{
dgvRow [columnPos] = objx.Value;
}
else
{
Debug.Print(name +************* NOT FOUND ********* **);
}

if(name.Equals(chksum))
{
//将行添加到datagridview。
dataGridView1.Rows.Add(dgvRow);
dgvRow =新对象[dgvHeaders.Count];
Debug.Print(--------------------------------);
}
}
}
}
}
}


How do I feed the following XML to a datagrid view

<root type="object">
  <gstin type="string">24AB3584G1ZL</gstin>
  <fp type="string">082017</fp>
  <b2b type="array">
    <item type="object">
      <ctin type="string">24AC7232H1ZR</ctin>
      <cfs type="string">Y</cfs>
      <cname type="null"></cname>
      <inv type="array">
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">1476.75</samt>
                <rt type="number">5</rt>
                <txval type="number">59070</txval>
                <camt type="number">1476.75</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">62024</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">12-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">217/G</inum>
          <chksum type="string">fb9401722f9f9f46cfa8ddf70647ebd47a3ca1094f4c6a17e874aea79296dcb8</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">1408.38</samt>
                <rt type="number">5</rt>
                <txval type="number">56335</txval>
                <camt type="number">1408.38</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">59152</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">12-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">218/G</inum>
          <chksum type="string">d06e30bc81f3ea8f83b735104967f878119a635dd898b33e954f41d87ebacc8c</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">1393.5</samt>
                <rt type="number">5</rt>
                <txval type="number">55740</txval>
                <camt type="number">1393.5</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">58527</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">16-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">222/G</inum>
          <chksum type="string">1a5e45053bf0665455557ea3df6b0d4d170c595b646ed54d52ef561bb66bdf77</chksum>
        </item>
      </inv>
    </item>
    <item type="object">
      <ctin type="string">24AA3022L1ZJ</ctin>
      <cfs type="string">Y</cfs>
      <cname type="null"></cname>
      <inv type="array">
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">1</num>
              <itm_det type="object">
                <csamt type="number">0</csamt>
                <samt type="number">1240.7</samt>
                <rt type="number">5</rt>
                <txval type="number">49628</txval>
                <camt type="number">1240.7</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">52109</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">27-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">13/119</inum>
          <chksum type="string">5951d10d389e1b6d9f6b88c536b5fc5f3676b9dd15d977a3fbe741407bd3796f</chksum>
        </item>
      </inv>
    </item>
    <item type="object">
      <ctin type="string">24AI654J1ZX</ctin>
      <cfs type="string">Y</cfs>
      <cname type="null"></cname>
      <inv type="array">
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">1</num>
              <itm_det type="object">
                <csamt type="number">0</csamt>
                <samt type="number">1263.83</samt>
                <rt type="number">5</rt>
                <txval type="number">50553</txval>
                <camt type="number">1263.83</camt>
                <iamt type="number">0</iamt>
              </itm_det>
            </item>
          </itms>
          <val type="number">53080.65</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">23-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">149</inum>
          <chksum type="string">ed7e5e187ea6349faff81f7e2eb2910a87ec4df0d70f388aa1ba4b46312032ba</chksum>
        </item>
      </inv>
    </item>
    <item type="object">
      <ctin type="string">24A6274F1ZA</ctin>
      <cfs type="string">Y</cfs>
      <cname type="null"></cname>
      <inv type="array">
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">1</num>
              <itm_det type="object">
                <csamt type="number">0</csamt>
                <samt type="number">748.65</samt>
                <rt type="number">5</rt>
                <txval type="number">29946</txval>
                <camt type="number">748.65</camt>
                <iamt type="number">0</iamt>
              </itm_det>
            </item>
          </itms>
          <val type="number">31443</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">01-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">594</inum>
          <chksum type="string">c3c6c3b9651ded60d8cd58d5659aa99e5f08a50b04e261a647fdd2e55958c03c</chksum>
        </item>
      </inv>
    </item>
    <item type="object">
      <ctin type="string">24AA064Q1ZW</ctin>
      <cfs type="string">Y</cfs>
      <cname type="null"></cname>
      <inv type="array">
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">649.45</samt>
                <rt type="number">5</rt>
                <txval type="number">25978</txval>
                <camt type="number">649.45</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">27277</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">04-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">5685/J</inum>
          <chksum type="string">e4809d5e25b7df166fe140851c6d74244807c8de759bfd251cf9f2f06a4fabf5</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">136.8</samt>
                <rt type="number">5</rt>
                <txval type="number">5472</txval>
                <camt type="number">136.8</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">5746</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">05-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">5721/J</inum>
          <chksum type="string">1244f4d49e7a3e50f42bb9b0f87ebd961d4a7d0900c84d5d1feff9788c27b151</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">130.97</samt>
                <rt type="number">5</rt>
                <txval type="number">5239</txval>
                <camt type="number">130.97</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">5501</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">05-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">5753/J</inum>
          <chksum type="string">1eb028428be75cfdc790a0de2f7a9197099166617b46e7a4c9e4a31de17b194f</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">415.8</samt>
                <rt type="number">5</rt>
                <txval type="number">16632</txval>
                <camt type="number">415.8</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">17464</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">08-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">5826/J</inum>
          <chksum type="string">c174a888d8f2ba95c78354ebefcb41d75b37902daf7ca43c2f2b2e022e501dde</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">272.8</samt>
                <rt type="number">5</rt>
                <txval type="number">10912</txval>
                <camt type="number">272.8</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">11458</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">09-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">5866/J</inum>
          <chksum type="string">b1fce912197d109cf4bf19f7b585bbffabe7bcd53772369ce2a2935a0d4162bc</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">429.35</samt>
                <rt type="number">5</rt>
                <txval type="number">17174</txval>
                <camt type="number">429.35</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">18033</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">21-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">6437/J</inum>
          <chksum type="string">3577ce8549a7e8853eb00b0737b2f94352abbbd70f6a12946dc803d4a245895a</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">288.7</samt>
                <rt type="number">5</rt>
                <txval type="number">11548</txval>
                <camt type="number">288.7</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">12125</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">22-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">6470/J</inum>
          <chksum type="string">ef6104e50fceec2e841aaccd2814e522b2c06803fddde53cf2a343e7a551eb32</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">1515.53</samt>
                <rt type="number">5</rt>
                <txval type="number">60621</txval>
                <camt type="number">1515.53</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">63652</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">26-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">6796/J</inum>
          <chksum type="string">8b666418ab813d2030d882e0f18071da91d83adfbe6d7b0ac557d70aad0877ca</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">1360.13</samt>
                <rt type="number">5</rt>
                <txval type="number">54405</txval>
                <camt type="number">1360.13</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">57125</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">28-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">6897/J</inum>
          <chksum type="string">c8fb4ae410a3283c5221e9203167dc1687791a87179850c51a0f15346b317541</chksum>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">342.73</samt>
                <rt type="number">5</rt>
                <txval type="number">13709</txval>
                <camt type="number">342.73</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">14394</val>
          <inv_typ type="string">R</inv_typ>
          <pos type="string">24</pos>
          <idt type="string">29-08-2017</idt>
          <rchrg type="string">N</rchrg>
          <inum type="string">6931/J</inum>
          <chksum type="string">07ff8bcde46c86e427a2fa850c58d643f2adc32182ec674091c94f2419a787dd</chksum>
        </item>
      </inv>
    </item>
  </b2b>
  <cdn type="array">
    <item type="object">
      <cfs type="string">Y</cfs>
      <ctin type="string">24A0064Q1ZW</ctin>
      <cname type="null"></cname>
      <nt type="array">
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">59.23</samt>
                <rt type="number">5</rt>
                <txval type="number">2369</txval>
                <camt type="number">59.23</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">2487</val>
          <idt type="string">26-08-2017</idt>
          <ntty type="string">C</ntty>
          <nt_num type="string">65</nt_num>
          <inum type="string">6796/J</inum>
          <rsn type="string">02-Post Sale Discount</rsn>
          <nt_dt type="string">31-08-2017</nt_dt>
          <chksum type="string">ec636db911bbcf11b1aa200c91fe72ee4850e74886530247dc7f54353b912c93</chksum>
          <p_gst type="string">N</p_gst>
        </item>
        <item type="object">
          <itms type="array">
            <item type="object">
              <num type="number">500</num>
              <itm_det type="object">
                <samt type="number">353.98</samt>
                <rt type="number">5</rt>
                <txval type="number">14159</txval>
                <camt type="number">353.98</camt>
              </itm_det>
            </item>
          </itms>
          <val type="number">14867</val>
          <idt type="string">28-08-2017</idt>
          <ntty type="string">C</ntty>
          <nt_num type="string">74/C</nt_num>
          <inum type="string">6897/J</inum>
          <rsn type="string">02-Post Sale Discount</rsn>
          <nt_dt type="string">31-08-2017</nt_dt>
          <chksum type="string">4cf679fd864f55bcc9c4eafb039deddeb6df8ed97b495f4023548946c884b39e</chksum>
          <p_gst type="string">N</p_gst>
        </item>
      </nt>
    </item>
  </cdn>
</root>



What I have tried:

I have tried DataSet.ReadXML but it''s not working since there is a common parent and child table.



Need it in this format
RequiredOutput.csv - Google Drive[^]

解决方案

If your XML is not in a correct format, you will need to use an XmlReader to read it line by line, see example here: [Dotnetperls]
Painful and unwieldy, but that''s the way it is I''m afraid :)


I''d strogly recommend to use xsd.exe tool[^], which helps to create classes from xml. Then you''ll be able to use XmlSerialization[^] to convert xml data into object and vice versa.

Note: xsd tool is available in Microsoft SDK folder. See: xml - where to find xsd.exe in visual studio 2013 on windows 8 - Stack Overflow[^]

For further details, please see:
How to: Use the XML Schema Definition Tool to Generate Classes and XML Schema Documents | Microsoft Docs[^]
Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization – Yogesh Joshi''s Blog[^]
XML Serialization – Tips & Tricks[^]
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]


I finally cracked it, the trick is to use recursion, here is the updated dynamic version:

using System.Windows.Forms;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Linq;
using System;

namespace DataGridViewXml
{
    public partial class Form2 : Form
    {
        private string xmlpath = @"test.xml";
        public Dictionary<string, int> dgvHeaders;
        public BindingSource dgvSource;
        public object[] dgvRow;

        public Form2()
        {
            InitializeComponent();
            this.Run();
        }

        public void Run()
        {
            dgvHeaders = new Dictionary<string, int>();
            XElement xelement = XElement.Load(xmlpath);

            GetHeaders(xelement);

            foreach (var header in dgvHeaders)
            {
                Debug.Print(header.Key.PadRight(20) +  header.Value);
                dataGridView1.Columns.Add(header.Key, header.Key);
            }

            dgvRow = new object[dgvHeaders.Count];
            RecurseElements(xelement);
        }

        private void GetHeaders(XElement xelement)
        {
            foreach (var objx in xelement.Elements())
            {
                if (objx.HasElements)
                {
                    GetHeaders(objx);
                }
                else
                {
                    string header = objx.Name.ToString();

                    if (!dgvHeaders.ContainsKey(header))
                    {
                        // Add header only if it is not in the dictionary already.
                        dgvHeaders.Add(header, dgvHeaders.Count);
                    }
                }
            }
        }

        private void RecurseElements(XElement xelement)
        {
            foreach (var objx in xelement.Elements())
            {
                if (objx.HasElements)
                {
                    RecurseElements(objx);
                }
                else
                {
                    string name = objx.Name.ToString();
                    Debug.Print(name + " = " + objx.Value.ToString());

                    int columnPos = 0;
                    dgvHeaders.TryGetValue(name, out columnPos);

                    // Construct one row.
                    if (columnPos >= 0)
                    {
                        dgvRow[columnPos] = objx.Value;
                    }
                    else
                    {
                        Debug.Print(name + "   ************* NOT FOUND ***********");
                    }

                    if (name.Equals("chksum"))
                    {
                        // Add the row to the datagridview.
                        dataGridView1.Rows.Add(dgvRow);
                        dgvRow = new object[dgvHeaders.Count];
                        Debug.Print("--------------------------------");
                    }
                }
            }
        }
    }
}


这篇关于将XML提供给datagridview C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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