用于将XML文件读入DataTable的代码
我编写了下面的代码片段,它读取给定的xml文件并将内容写入数据表。 请不要建议使用LinqToXml,因为这是一个遗留应用程序。
// 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; }
但是返回的表包含ZERO行…… !!! 其中实际的xml文件有’3行’,结构如下(任何IDEA这里出了什么问题?):
Jack C:\Users\Public\Pictures\Sample Pictures\Desert.jpg NewYork Tom C:\Users\Public\Pictures\Sample Pictures\Desert.jpg London Jill C:\Users\Public\Pictures\Sample Pictures\Desert.jpg Tokyo
您可以使用ReadXML
DataSet ds = new DataSet(); ds.ReadXml(fileNameWithAbsolutePath); return ds.Tables[0];
- 无法在unit testing项目中使用ConfigurationManager
- 在WPF DataTrigger值中绑定
- log4net BufferingForwardingAppender性能问题
- 如何使用asp.net和c#从Web应用程序发送短信到手机?
- 克隆Linq对象错误“类型’TestLinq.PersonAddress’的对象图包含循环,如果禁用参考跟踪,则无法序列化。”
- System.DirectoryServices.DirectoryServicesCOMException(0x800700EA):有更多数据可用
- 使用适用于.NET的YouTube v3 Data API,如何获得刷新令牌?
- 从Web下载之前获取图像文件的大小
- 如何使用Unity的名称动态注册generics类?