c# - XmlReader 和 MemoryStream,返回的 xml 缺少标签

标签 c# xml xmlreader memorystream

有人可以向我解释一下这种行为吗?

如果您使用第一个字符串执行帖子底部的代码片段,它会返回与用于输入的字符串完全相同的字符串;这正是我所期望的。

输入 1:

<?xml version='1.0' encoding='UTF-8'?>
<Company>
  <Creator>Me</Creator>
  <CreationDateTime>2010-01-25T21:58:32.493</CreationDateTime>
  <Contacts>
    <Contact>
      <ContactID>365</ContactID>
    </Contact>
  </Contacts>
</Company>

输出1:

<?xml version='1.0' encoding='UTF-8'?>
<Company>
  <Creator>Me</Creator>
  <CreationDateTime>2010-01-25T21:58:32.493</CreationDateTime>
  <Contacts>
    <Contact>
      <ContactID>365</ContactID>
    </Contact>
  </Contacts>
</Company>

现在,如果您使用第二行 (const string xml),它是完全相同的字符串,但在一行而不是两行,它返回以下内容

输入 2

<?xml version='1.0' encoding='UTF-8'?>
<Company>
  <Creator>Me</Creator>
  <CreationDateTime>2010-01-25T21:58:32.493</CreationDateTime>
  <Contacts>
    <Contact>
      <ContactID>365</ContactID>
    </Contact>
  </Contacts>
</Company>

输出 2

<?xml version='1.0' encoding='UTF-8'?>
<Creator>Me</Creator>2010-01-25T21:58:32.493 
<Contacts>
  <Contact>
    <ContactID>365</ContactID>
  </Contact>
</Contacts>

两者之间的唯一区别是第一个在 xml 声明之后有一个换行符,但如您所见,第二个输出缺少 Parent 标记和第三个标记。有什么想法吗?

这是我使用的代码:

public void XmlReader_Eats_Tags_IsTrue()
    {
        //this first xml declaration is on two lines - line break is right after the xml declaration (I am not sure how to add the line break using the markdown, so if you execute the code on your machine, please add it)
        const string xml = @"<?xml version='1.0' encoding='UTF-8'?><Company><Creator>Me</Creator><CreationDateTime>2010-01-25T21:58:32.493</CreationDateTime><Contacts><Contact><ContactID>365</ContactID></Contact></Contacts></Company>";

        //The seconde xml declaration is on one line
        //const string xml = @"<?xml version='1.0' encoding='UTF-8'?><Company><Creator>Me</Creator><CreationDateTime>2010-01-25T21:58:32.493</CreationDateTime><Contacts><Contact><ContactID>365</ContactID></Contact></Contacts></Company>";

        BufferedStream stream = new BufferedStream(new MemoryStream());
        stream.Write(Encoding.ASCII.GetBytes(xml), 0, xml.Length);
        stream.Seek(0, SeekOrigin.Begin);
        StreamReader streamReaderXml = new StreamReader(stream);

        XmlReader xmlR = XmlReader.Create(streamReaderXml);

        XmlReaderSettings xmlReaderset = 
                         new XmlReaderSettings{ValidationType = ValidationType.Schema};
        xmlReaderset.Schemas.ValidationEventHandler += ValidationCallBack;

        MemoryStream ms = new MemoryStream();
        XmlWriterSettings xmlWriterSettings = 
                          new XmlWriterSettings{
                                  Encoding = new UTF8Encoding(false),
                                  ConformanceLevel = ConformanceLevel.Fragment
                          };

        using (XmlWriter xmlTw = XmlWriter.Create(ms, xmlWriterSettings))
        {
            using (XmlReader xmlRead = XmlReader.Create(xmlR, xmlReaderset))
            {
                int i = 0;
                while (xmlRead.Read())
                {
                    Console.WriteLine("{0}:{1}; node type: {2}", i, xmlRead.Name, xmlRead.NodeType);
                    // Reads the whole file and will call the validation handler subroutine if an error is detected.
                    xmlTw.WriteNode(xmlRead, true);
                    i++;
                }

                xmlTw.Flush();
                xmlRead.Close();
            }
            string xmlString = Encoding.UTF8.GetString(ms.ToArray());
            Console.WriteLine(xmlString);
        }
    }

最佳答案

问题是您正在使用 XmlWriter.WriteNode(reader, true) 并且调用XmlReader.Read()WriteNode 已经将读取器移动到同级元素上,因此当您再次调用 Read 时实际上是在跳过数据。

我怀疑它碰巧在第一个版本中工作,因为您在第二次调用 Read 时跳过了空格,然后阅读了文档的其余部分在对 WriteNode 的第二次调用中。

关于c# - XmlReader 和 MemoryStream,返回的 xml 缺少标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2199904/

相关文章:

c# - 从 URL 读取 XML 文件,结果为空

javascript - Bootstrap 模式加载远程内容生成 2 个请求

c# - 上传文件数功能不显示错误消息

xml - 使用 xsd 将 csv 转换为 xml

python - 使用python将xml节点添加到xml文件

PHP xml阅读器404错误

c# - 使用分组依据以删除重复项

c# - 如何正确地将 DataReader 转换为 DTO/List<DTO>?

ruby-on-rails - 使用 Ruby on Rails (1.4GB) 解析非常大的 XML 文件——有没有比 SAXParser 更好的方法?

c# - XmlReader 在 C# 中的工作原理