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

查看:30
本文介绍了将 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天全站免登陆