C# XmlReader和MemoryStream,返回的xml未命中标记

C# XmlReader和MemoryStream,返回的xml未命中标记,c#,xml,xmlreader,memorystream,C#,Xml,Xmlreader,Memorystream,有人能给我解释一下这种行为吗 如果使用第一个字符串执行文章底部的代码段,它将返回与用于输入的字符串完全相同的字符串;这正是我所期望的 投入1: <?xml version='1.0' encoding='UTF-8'?> <Company> <Creator>Me</Creator> <CreationDateTime>2010-01-25T21:58:32.493</CreationDateTime> <

有人能给我解释一下这种行为吗

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

投入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>
产出2

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

Me2010-01-25T21:58:32.493
365
2之间的唯一区别是,第一个输出在xml声明之后有一个换行符,但正如您所看到的,第二个输出遗漏了父标记和第三个标记。有什么想法吗

以下是我使用的代码:

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);
        }
    }
public void XmlReader\u Eats\u Tags\u IsTrue()
{
//第一个xml声明有两行-换行符就在xml声明之后(我不知道如何使用标记添加换行符,因此如果您在计算机上执行代码,请添加它)
常量字符串xml=@“Me2010-01-25T21:58:32.493365”;
//第二个xml声明位于一行
//常量字符串xml=@“Me2010-01-25T21:58:32.493365”;
BufferedStream=新的BufferedStream(new MemoryStream());
stream.Write(Encoding.ASCII.GetBytes(xml),0,xml.Length);
stream.Seek(0,SeekOrigin.Begin);
StreamReader streamReaderXml=新的StreamReader(流);
XmlReader xmlR=XmlReader.Create(streamReaderXml);
XmlReaderSettings xmlReaderset=
新的XmlReaderSettings{ValidationType=ValidationType.Schema};
xmlReaderset.Schemas.ValidationEventHandler+=ValidationCallBack;
MemoryStream ms=新的MemoryStream();
XmlWriterSettings XmlWriterSettings=
新的XmlWriterSettings{
编码=新的UTF8编码(错误),
ConformanceLevel=ConformanceLevel.Fragment
};
使用(XmlWriter xmlTw=XmlWriter.Create(ms,xmlWriterSettings))
{
使用(XmlReader xmlRead=XmlReader.Create(xmlR,xmlReaderset))
{
int i=0;
while(xmlRead.Read())
{
WriteLine(“{0}:{1};节点类型:{2}”,i,xmlRead.Name,xmlRead.NodeType);
//读取整个文件,并在检测到错误时调用验证处理程序子例程。
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
时读取了文档的其余部分。您完全正确;如果我将
XmlReaderSettings
IgnoreWhitespace
属性设置为
true
,则两个示例都会跳过标记。谢谢你的启发
<?xml version='1.0' encoding='UTF-8'?>
<Creator>Me</Creator>2010-01-25T21:58:32.493 
<Contacts>
  <Contact>
    <ContactID>365</ContactID>
  </Contact>
</Contacts>
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);
        }
    }