有人可以向我解释这种行为吗?
如果您使用第一个字符串执行帖子底部的片段,它将返回与用于输入的字符串完全相同的字符串;这就是我的预期。
输入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>
2之间的唯一区别是第一个在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);
}
}
答案 0 :(得分:6)
问题是您正在使用XmlWriter.WriteNode(reader, true)
和调用XmlReader.Read()
。 WriteNode
已将读者移动到同级元素上,因此当您再次调用Read
时,您实际上正在跳过数据。
我怀疑发生在第一个版本中工作,因为你在第二次调用Read
时跳过空格,然后在第二个版本中读取剩下的文档致电WriteNode
。