我有一个包含XML数据的文本字符串(虽然它没有标题信息),我试图填充包含其元素的DataTable。我没有收到任何错误消息,但是我的DataTable在.ReadXml命令后面包含0行。 Xml如下:
<items>
<item>
<Date>05-Feb-2008</Date>
<User>Unknown</User>
<Comment>Due date moved</Comment>
<Restricted>True</Restricted>
</item>
<item>
<Date>07-Nov-2008</Date>
<User>Unknown</User>
<Comment>Priority increased. Due date moved to end Oct</Comment>
<Restricted>False</Restricted>
</item>
</items>
我创建DataTable的代码如下:
Stream xmlStream = new MemoryStream();
StreamWriter writer = new StreamWriter(xmlStream);
string xml = (string)row["XmlComments"];
writer.Write(xml);
writer.Flush();
xmlStream.Position = 0;
DataTable xmlComments = new DataTable();
xmlComments.Columns.Add("User", typeof(string));
xmlComments.Columns.Add("Date", typeof(DateTime));
xmlComments.Columns.Add("Comment", typeof(string));
xmlComments.Columns.Add("OSG_Only", typeof(string));
xmlComments.ReadXml(xmlStream);
有人能否解释我在这里缺少的东西?
非常感谢
答案 0 :(得分:1)
这应该有效
DataTable xmlComments = new DataTable();
xmlComments.TableName = "item"; //<---- Added
xmlComments.Columns.Add("User", typeof(string));
xmlComments.Columns.Add("Date", typeof(string)); //<---- Type changed
xmlComments.Columns.Add("Comment", typeof(string));
xmlComments.Columns.Add("Restricted", typeof(string)); //<---- Column name changed
xmlComments.ReadXml(xmlStream);