0

我想将 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 结构)

在此处输入图像描述

4

2 回答 2

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
于 2013-03-07T10:26:36.600 回答
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
于 2013-03-07T10:18:20.543 回答