0

I have a XML column in a table contains a collection of data. Every record may hold a different collection type like sometimes customers data and sometimes invoices data etc.

How can I read that cell & convert it to a table in order to bind it to a data grid, so the collection some times looks like that

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

And sometimes looks like

<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

2 Answers 2

1

You already have one root tag so this will also work

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

        }
    }
}
​
Sign up to request clarification or add additional context in comments.

Comments

0

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.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];

        }
    }
}
​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.