读取方法跳过节点

时间:2012-07-30 20:19:48

标签: c# xml

我无法尝试解析XML文件,因为Read()方法似乎正在跳过某些行。首先,这是XML文件:

  <comments>
    <comment id="e2d6d918-0fbb-4434-9e43-6b028b872867" parentid="00000000-0000-0000-0000-000000000000" approved="True" spam="False" deleted="False">
      <date>2012-06-14 18:40:55</date>
      <author>*content here*</author>
      <email>*content here*</email>
      <country>*content here*</country>
      <ip>*content here*</ip>
      <moderatedby>*content here*</moderatedby>
      <content>*content here*</content>
    </comment>
    <comment id="7f74b8af-73e5-407f-abcb-c3cba5ffc611" parentid="e2d6d918-0fbb-4434-9e43-6b028b872867" approved="True" spam="False" deleted="False">
      <date>2012-06-15 01:59:34</date>
      <author>*content here*</author>
      <email>*content here*</email>
      <country>*content here*</country>
      <ip>*content here*</ip>
      <website>*content here*</website>
      <content>*content here*</content>
    </comment>
  </comments>

关于代码:

(我正在使用XmlTextReader

            while (reader.Read())
            {
                if (reader.Name == "comment" && reader.NodeType == XmlNodeType.Element)
                {
                    Comment newComment = new Comment();

                    newComment.PostId = postId;

                    reader.MoveToAttribute("deleted");
                    newComment.IsDeleted = Convert.ToBoolean(reader.Value);
                    reader.MoveToAttribute("spam");
                    newComment.IsSpam = Convert.ToBoolean(reader.Value);
                    reader.MoveToAttribute("approved");
                    newComment.IsApproved = Convert.ToBoolean(reader.Value);
                    reader.MoveToAttribute("parentid");
                    newComment.ParentCommentId = new Guid(reader.Value);
                    reader.MoveToAttribute("id");
                    newComment.PostCommentId = new Guid(reader.Value);

                    reader.ReadToFollowing("date");
                    newComment.CommentDate = Convert.ToDateTime(reader.ReadString());

                    reader.ReadToFollowing("author");
                    newComment.Author = reader.ReadString();

                    reader.ReadToFollowing("email");
                    newComment.Email = reader.ReadString();

                    reader.ReadToFollowing("country");
                    newComment.Country = reader.ReadString();

                    reader.ReadToFollowing("ip");
                    newComment.Ip = reader.ReadString();

                    reader.ReadToFollowing("website");
                    newComment.Website = reader.ReadString();

                    //reader.ReadToFollowing("moderatedBy");
                    newComment.ModeratedBy = string.Empty;

                   //reader.ReadToFollowing("avatar");
                    newComment.Avatar = string.Empty;

                    reader.ReadToFollowing("content");
                    newComment.CommentContent = reader.ReadString();

                    comments.Add(newComment);
                }
            }

编辑:我做了一些调试,发现我正在尝试阅读不存在的网站元素。

1 个答案:

答案 0 :(得分:0)

问题在于我试图阅读所有评论节点都不存在的“网站”元素。这导致读者继续搜索,直到它找到一个(就像它在上面显示的XML文件中通过转到第二个注释那样)或到达文件的末尾。

我删除了该支票,现在可以使用了。

相关问题