【问题标题】:XML Stream returning zero rows返回零行的 XML 流
【发布时间】:2012-11-28 23:38:17
【问题描述】:

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

我创建数据表的代码如下:

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);

有人能解释一下我在这里缺少什么吗?

非常感谢

【问题讨论】:

    标签: c# xml


    【解决方案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);
    

    【讨论】:

    • 太棒了,已经解决了。我想知道是否需要在某处指定“项目”,我可能应该在发布之前尝试过。此外,该字段实际上被称为“OSG_Only”我刚刚尝试并未能从我的帖子中删除对它的引用!非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    相关资源
    最近更新 更多