Reputation: 343
Recently, I replaced XmlValidatingReader with XmlReader and use XmlReaderSettings to validate the xml with an xsd.
CustomData cd = null;
using (Stream xsdStream = new FileStream(m_DXsdFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
XmlTextReader sr = new XmlTextReader(xsdStream);
using (Stream xmlStream = new FileStream(m_DXmlFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, sr);
XmlReader reader = XmlReader.Create(xmlStream, settings);
try
{
XmlSerializer xmlSerializer = CreateDefaultXmlSerializer(m_Type);
cd = (CustomData)xmlSerializer.Deserialize(reader);
}
...
}
XML:
<Column Name="Content">
<Data xsi:type="xsd:string"> * info info info
info info info
info info info</Data>
<Data xsi:type="xsd:string">/* This is a test */</Data>
</Column>
<Column Name="ErrorNr">
<Data xsi:type="xsd:int">2</Data>
<Data xsi:type="xsd:int">2</Data>
<Data xsi:type="xsd:int">2</Data>
</Column>
CreateDefaultXmlSerializer() is implemented as suggested here: https://stackoverflow.com/a/9416860/2107510
However, XmlReader normalizes all newlines and converts \r\n to \n. This is not appreciated by the multi line text boxes which now display one line instead of multiple lines.
I tried using XmlWriterSettings.NewLineHandling.Entitize and it fixes my problem but this make the XML document no longer readable in e.g. wordpad or any other text editor. (http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlinehandling.aspx)
How can I preserve the multi line strings without losing them due to normalization? (and without other nasty side effects)
Upvotes: 3
Views: 1631
Reputation: 6586
From Creating Xml Readers (emphasis mine):
If you must expand entities on request (readers created by the Create method expand all entities), or if you do not want your text content normalized, use the
XmlTextReader
class.
Upvotes: 1