2
我需要能够用一个文件来做到这一点,我不一定知道元素名称是什么,或者他们有多少孩子将有(即:dataTable将有多少列)。然而,它的格式应该是:将XML读入一个DataTable - System.Xml.XmlException:根元素缺失
<Items>
<Item>
<Price>25</Price>
<Name>Basketball</Name>
</Item>
<Item>
<Price>29</Price>
<Name>Football</Name>
</Item>
</Items>
我已经试过这样:
DataSet ds = new DataSet();
ds.ReadXml(file.PostedFile.InputStream, XmlReadMode.InferSchema);
这:
ds.Tables[0].ReadXml(file.PostedFile.InputStream);
但我不断收到“System.Xml.XmlException:根元素缺失。“
从您的示例xml创建'DataSet'没有任何问题(实际上,您缺少关闭价格标签,但我认为是它的错字) –
要解决问题,请尝试将文件读入字符串并查看会发生什么in:'string troubleshoot = new StreamReader(file.PostedFile.InputStream).ReadAllText();' –
@Alfie - '<?xml version =“1.0”encoding =“ISO-8859-1”?>不是根元件。该结构被称为'XML声明'。在上面的示例中,“”元素是根元素。 –
pmartin