1

我有一个包含 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);

任何人都可以阐明我在这里缺少的东西吗?

非常感谢

4

1 回答 1

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);
于 2012-11-28T15:38:17.000 回答