我在表中有一个包含数据集合的XML列。每条记录可能包含不同的集合类型,例如客户数据,有时还有发票数据等。
我怎样才能阅读那个细胞&将其转换为表以将其绑定到数据网格,因此集合有时看起来像
<ArrayOfReceiptTransfer_Receipt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ReceiptTransfer_Receipt>
<ReceiptTransfer_Receipt_ID>77491</ReceiptTransfer_Receipt_ID>
<ReceiptTransferID>17839</ReceiptTransferID>
<ReceiptID>74080</ReceiptID>
<Amount>500.00</Amount>
</ReceiptTransfer_Receipt>
</ArrayOfReceiptTransfer_Receipt>
有时看起来像
<ArrayOfInvoiceBudgetItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<InvoiceBudgetItem>
<InvoiceID>21978</InvoiceID>
<BudgetItemID>1473</BudgetItemID>
<Amount>12</Amount>
</InvoiceBudgetItem>
<InvoiceBudgetItem>
<InvoiceID>21978</InvoiceID>
<BudgetItemID>1475</BudgetItemID>
<Amount>11</Amount>
</InvoiceBudgetItem>
</ArrayOfInvoiceBudgetItem>
答案 0 :(得分:1)
您已经拥有一个根标记,因此也可以使用
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.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string input =
"<ArrayOfReceiptTransfer_Receipt xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"<ReceiptTransfer_Receipt>" +
"<ReceiptTransfer_Receipt_ID>77491</ReceiptTransfer_Receipt_ID>" +
"<ReceiptTransferID>17839</ReceiptTransferID>" +
"<ReceiptID>74080</ReceiptID>" +
"<Amount>500.00</Amount>" +
"</ReceiptTransfer_Receipt>" +
"</ArrayOfReceiptTransfer_Receipt>";
string xml = string.Format("<?xml version=\"1.0\" encoding=\"utf-8\"?>{0}", input);
StringReader reader = new StringReader(xml);
DataSet ds = new DataSet();
ds.ReadXml(reader);
dataGridView1.DataSource = ds.Tables[0];
}
}
}
答案 1 :(得分:0)
试试这个
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.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string input =
"<ArrayOfReceiptTransfer_Receipt xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"<ReceiptTransfer_Receipt>" +
"<ReceiptTransfer_Receipt_ID>77491</ReceiptTransfer_Receipt_ID>" +
"<ReceiptTransferID>17839</ReceiptTransferID>" +
"<ReceiptID>74080</ReceiptID>" +
"<Amount>500.00</Amount>" +
"</ReceiptTransfer_Receipt>" +
"</ArrayOfReceiptTransfer_Receipt>";
string xml = string.Format("<?xml version=\"1.0\" encoding=\"utf-8\"?><root>{0}</root>", input);
StringReader reader = new StringReader(xml);
DataSet ds = new DataSet();
ds.ReadXml(reader);
dataGridView1.DataSource = ds.Tables[1];
}
}
}