代码读取XML文件转换成一个DataTable [英] Code for reading an XML file into a DataTable

查看:366
本文介绍了代码读取XML文件转换成一个DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的一段代码,通过一个给定的XML的文件读取内容写入到数据表。请不要建议使用LinqToXml作为选项被排除,因为这是传统应用程序。

  //创建数据表即会保存数据
DataTable的表=新的DataTable(ListOfPersonsWithInfo);


//使用流
使用打开文件(流流=新的FileStream(fileNameWithAbsolutePath,FileMode.Open,FileAccess.Read))
{
//创建一个相应的列名
table.Columns.Add(名,typeof运算(字符串))表;
table.Columns.Add(的ImagePath,typeof运算(字符串));
table.Columns.Add(地址,typeof运算(字符串));

//使用的ReadXml读取XML流
table.ReadXml(流);

//使用这个重载选项尝试很好,但没有帮助
//table.ReadXml(fileNameWithAbsolutePath);

//返回结果
返回表;
}



,但返回的表包含零行...! !其中,作为实际的XML文件中有'3行'和结构如下(任何想法是怎么回事错在这里?):



 <?XML版本=1.0编码=UTF-8>?; 
<详情>
< EachPerson>
<名称>杰克LT; /名称>
< ImagePathAndFileName> C:\Users\Public\Pictures\Sample Pictures\Desert.jpg< / ImagePathAndFileName>
<地址>&纽约LT; /地址>
< / EachPerson>
< EachPerson>
<名称>汤姆和LT; /名称>
< ImagePathAndFileName> C:\Users\Public\Pictures\Sample Pictures\Desert.jpg< / ImagePathAndFileName>
<地址>伦敦和LT; /地址>
< / EachPerson>
< EachPerson>
<名称>&吉尔LT; /名称>
< ImagePathAndFileName> C:\Users\Public\Pictures\Sample Pictures\Desert.jpg< / ImagePathAndFileName>
<地址>&东京LT; /地址>
< / EachPerson>
< /详细资料及GT;


解决方案

您可以使用ReadXML的

 的DataSet DS =新的DataSet(); 
ds.ReadXml(fileNameWithAbsolutePath);
返回ds.Tables [0];


I have written the following piece of code that reads through a given xml-file and writes the contents into a data-table. Please do NOT suggest to use LinqToXml as that option is ruled because this is a legacy application.

            // create the DataTable that will hold the data
            DataTable table = new DataTable("ListOfPersonsWithInfo");


            // open the file using a Stream
            using (Stream stream = new FileStream(fileNameWithAbsolutePath, FileMode.Open, FileAccess.Read))
            {
                // create the table with the appropriate column names
                table.Columns.Add("Name", typeof(String));
                table.Columns.Add("ImagePath", typeof(String));
                table.Columns.Add("Address", typeof(String));

                // use ReadXml to read the XML stream
                table.ReadXml(stream);

                // tried with this overload-option as well but didnt help
                //table.ReadXml(fileNameWithAbsolutePath);

                // return the results
                return table;
            }

BUT the returned-table contains ZERO rows...!!! where as the actual xml file has '3 rows' and is structured as follows (ANY IDEA what is going wrong here?):

<?xml version="1.0" encoding="utf-8"?>
<Details>
    <EachPerson>
        <Name>Jack</Name>
        <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName>
        <Address>NewYork</Address>
    </EachPerson>
    <EachPerson>
        <Name>Tom</Name>
        <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName>
        <Address>London</Address>
    </EachPerson>
    <EachPerson>
        <Name>Jill</Name>
        <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName>
        <Address>Tokyo</Address>
    </EachPerson>
</Details>

解决方案

You can use ReadXML

        DataSet ds = new DataSet();
        ds.ReadXml(fileNameWithAbsolutePath);
        return ds.Tables[0];

这篇关于代码读取XML文件转换成一个DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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