XML到Datatable的转换失败

时间:2013-03-07 10:03:20

标签: asp.net xml datatable

我想将XML字符串转换为Datatable.String就像这样

<TextstringArray>
    <values>
        <value>athul</value>
        <value>aks@phases.dk</value>
        <value>1</value>
    </values>
    <values>
        <value>arun</value>
        <value>am@phases.dk</value>
        <value>1</value>
    </values>
    <values>
        <value>ajmal</value>
        <value>am@phases.dk</value>
        <value>1</value>
    </values>
</TextstringArray>

我尝试过这样的事情

StringReader theReader = new StringReader(invitations);
DataSet theDataSet = new DataSet();
theDataSet.ReadXml(theReader);

但是数据集出现了错误格式化的数据。 就像所有的值元素都在单列中一样。我希望它们分为三列。一个用于第一列,依此类推。(Xml进入表而不是xml结构)

enter image description here

2 个答案:

答案 0 :(得分:1)

为了实现您的目标,XML应具有以下结构:

<TextstringArray>
    <values>
        <value1>athul</value1>
        <value2>aks@phases.dk</value2>
        <value3>1</value3>
    </values>
    <values>
        <value1>arun</value1>
        <value2>am@phases.dk</value2>
        <value3>1</value3>
    </values>
    <values>
        <value1>ajmal</value1>
        <value2>am@phases.dk</value2>
        <value3>1</value3>
    </values>
</TextstringArray>

这将生成一个单一的数据表,其中每个values元素将成为数据行的源,同时将读取其中的每个子元素(value1value2等)作为专栏。

解决方法是:

StringReader theReader = new StringReader(File.ReadAllText(invitations));
DataSet theDataSet = new DataSet();
theDataSet.ReadXml(theReader);

var valueIdsDatatable = theDataSet.Tables[0];
var valueDatatable = theDataSet.Tables[1];

// detect the maximum number of columns
var maxColumns = valueDatatable.AsEnumerable()
    .GroupBy(i => i["values_Id"]).Max(i => i.Count());

// create the result DataTable
var resultDataTable = new DataTable();

// add dynamically the columns
for (int i = 0; i < maxColumns; i++)
{
    resultDataTable.Columns.Add("property" + i);
}

// add the rows
foreach (DataRow valueId in valueIdsDatatable.Rows)
{
    var newRow = resultDataTable.NewRow();
    var currentRows = valueDatatable.Select("values_id = " + valueId[0]);
    for (int i = 0; i < currentRows.Length; i++)
    {
        newRow[i] = currentRows[i][0];
    }
    resultDataTable.Rows.Add(newRow);
}

// TODO: use the resultDataTable

答案 1 :(得分:0)

您可以使用一些LINQy优点来分组并投影到新的数据结构中。

请注意,以下代码期望每个组中只有3个值,并且依赖于输入是一致的。如果不是这种情况,您需要根据您的环境进行调整。

StringReader theReader = new StringReader(invitations);
DataSet theDataSet = new DataSet();
theDataSet.ReadXml(theReader);

// Get the table we are interested in.
var dt = theDataSet.Tables["value"];

// Group by the "values_Id" field. This is what logically 
// relates each <value>.
// Then, project out a new type, assuming that the first row 
// holds the username, the second holds the email address,
// and the third holds "some thing else".
var rows = dt.Rows.
    Cast<DataRow>().
    GroupBy(dr => dr["values_Id"]).
    Select(row => 
        new 
        {
            RowId = row.Key,
            UserName = row.ElementAt(0)["value_Text"],
            UserEmail = row.ElementAt(1)["value_Text"],
            UserOtherValue = row.ElementAt(2)["value_Text"]
        });

foreach (var row in rows)
{
    Console.WriteLine("Row " + row.RowId);
    Console.WriteLine("    UserName: " + row.UserName);
    Console.WriteLine("    Email: " + row.UserEmail);
    Console.WriteLine("    OtherValue: " + row.UserOtherValue);
}

产生以下输出:

Row 0
    UserName: athul
    Email: aks@phases.dk
    OtherValue: 1
Row 1
    UserName: arun
    Email: am@phases.dk
    OtherValue: 1
Row 2
    UserName: ajmal
    Email: am@phases.dk
    OtherValue: 1