加载XML字符串到数据表 [英] Loading XML String into datatable

查看:121
本文介绍了加载XML字符串到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

           string parseData= "<TEST xmlns:dt=\"urn:schemas-microsoft-com:datatypes\"><A dt:dt=\"string\">10</A><B dt:dt=\"string\">20</B></TEST>";

            DataSet ds = new DataSet("Whatev");

            TextReader txtReader = new StringReader(parseData);
            XmlReader reader = new XmlTextReader(txtReader);
            ds.ReadXml(reader);
            DataTable ad = new DataTable("newT");
            ad.ReadXml(reader);

有关这个我得到空表的广告,并在DS三个表。
我所期待的,
一个表有两列A和B,以及一排与值10和20。

For this I am getting empty table in ad, and three tables in ds. What I am expecting is, one table with two columns A and B, and one row with values 10 and 20.

我在做什么错了?

推荐答案

你根本无法正常使用。有一些问题:

You're simply not using it correctly. There's a number of problems:


  1. 您是从同一个流中读取两次。在第一遍中,流指针是在流的末尾。您尝试从中读取数据时再这样没有别的读它已经在最后。无论是读入数据集或到您的数据表,而不是两个。或者,至少设法回到流的开始,如果你真的想。

  1. You are reading from the same stream twice. In the first pass, the stream pointer is at the end of the stream. You attempt to read from it again when it's already at the end so nothing else is read. Either read into your data set or into your data table, not both. Or, at least seek back to the beginning of the stream if you really want to.

您的XML是不是在正确的格式。它是在格式为:

Your XML is not in the correct format. It has to be in the format:

<SomeRoot>
    <TableName>
        <ColumnName>ColumnValue</ColumnName>
    </TableName>
    <TableName>
        <ColumnName>AnotherColumnValue</ColumnName>
    </TableName>
</SomeRoot>

您将不能够使用任何任意的XML该方法

You won't be able to use that method with any arbitrary XML.

您的表没有一个模式集。你需要无论是在架构读取或设置它事先。

Your tables do not have a schema set. You need to either read in a schema or set it up beforehand.

var table = new DataTable("TEST");
table.Columns.Add("A", typeof(string));
table.Columns.Add("B", typeof(string));
table.ReadXml(xmlReader);


试试这个:

var xmlStr = @"<Root>
    <TEST>
        <A>10</A>
        <B>20</B>
    </TEST>
</Root>";
var table = new DataTable("TEST");
table.Columns.Add("A", typeof(string));
table.Columns.Add("B", typeof(string));
table.ReadXml(new StringReader(xmlStr));


如果你决定自己解析XML,LINQ可以帮助你在这里。


If you decide to parse that XML yourself, LINQ can help you out here.

public static DataTable AsDataTable(XElement root, string tableName, IDictionary<string, Type> typeMapping)
{
    var table = new DataTable(tableName);

    // set up the schema based on the first row
    XNamespace dt = "urn:schemas-microsoft-com:datatypes";
    var columns =
       (from e in root.Element(tableName).Elements()
        let typeName = (string)e.Element(dt + "dt")
        let type = typeMapping.ContainsKey(typeName ?? "") ? typeMapping[typeName] : typeof(string)
        select new DataColumn(e.Name.LocalName, type)).ToArray();
    table.Columns.AddRange(columns);

    // add the rows
    foreach (var rowElement in root.Elements(tableName))
    {
        var row = table.NewRow();
        foreach (var column in columns)
        {
            var colElement = rowElement.Element(column.ColumnName);
            if (colElement != null)
                row[column.ColumnName] = Convert.ChangeType((string)colElement, column.DataType);
        }
        table.Rows.Add(row);
    }
    return table;
}

然后使用它:

var xmlStr = @"<Root>
    <TEST xmlns:dt=""urn:schemas-microsoft-com:datatypes"">
        <A dt:dt=""string"">10</A>
        <B dt:dt=""string"">20</B>
    </TEST>
</Root>";
var root = XElement.Parse(xmlStr);
var mapping = new Dictionary<string, Type>
{
    { "string", typeof(string) },
};
var table = AsDataTable(root, "TEST", mapping);

有可能是一个更好的方式来得到一个数据类型关联的.NET类型,但我不知道该怎么做的那一刻,如果我发现了,我会更新。

There probably is a better way to get the associated .NET type for a datatype but I don't know how to do that at the moment, I'll update if I find that out.

这篇关于加载XML字符串到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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