我编写了以下代码,它读取给定的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这里出了什么问题?):
<?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>
答案 0 :(得分:11)
您可以使用ReadXML
DataSet ds = new DataSet();
ds.ReadXml(fileNameWithAbsolutePath);
return ds.Tables[0];