How should the "\ t" character in XML attribute values ​​be handled?

It seems I found something mismatch between the various XML implementations inside .NET 3.5, and I'm struggling to figure out what is nominally correct.

The problem is actually quite easy to reproduce:

  • Create a simple XML document with a text element containing the characters '\ t' and assign it an attribute containing the characters '\ t':

    var xmlDoc = new XmlDocument { PreserveWhitespace = false, };
    xmlDoc.LoadXml("<test><text attrib=\"Tab'\t'space' '\">Tab'\t'space' '</text></test>");
    xmlDoc.Save(@"d:\TabTest.xml");
    

    NB: This means that the XmlDocument itself is quite happy with the characters '\ t' in the attribute value.

  • Download the document using the new XmlTextReader:

    var rawFile = XmlReader.Create(@"D:\TabTest.xml");
    var rawDoc = new XmlDocument();
    rawDoc.Load(rawFile);
    
  • Download the document using XmlReader.Create:

    var rawFile2 = new XmlTextReader(@"D:\TabTest.xml");
    var rawDoc2 = new XmlDocument();
    rawDoc2.Load(rawFile2);
    
  • Compare documents in the debugger:

    (rawDoc).InnerXml   "<test><text attrib=\"Tab' 'space' '\">Tab'\t'space' '</text></test>"   string
    (rawDoc2).InnerXml  "<test><text attrib=\"Tab'\t'space' '\">Tab'\t'space' '</text></test>"  string
    

XmlTextReader , , "\ t" , . , , XmlReader.Create, , "\ t" ' '.

....!!: -)

After a bit of a Google search found that could encode a '\t' as '&#x9;' - if used this instead of '\t' in the example XML both readers work as expected.

Altova XmlSpy XML, -, '\ t' , - ?

XML "\ t", , XmlReader.Create, XML , "\ t" XmlReader.Create ?

, / ?

+5
4

@all: .

, Justin Michael Kay , W3C XML , MS .

XML , , , - - .

XmlWriter, XmlWriter.Create XML .

XmlDocument /, . , XmlWriter, .

, save code :

var xmlDoc = new XmlDocument { PreserveWhitespace = false, };
xmlDoc.LoadXml("<test><text attrib=\"Tab'\t'space' '\">Tab'\t'space' '</text></test>");

using (var xmlWriter = XmlWriter.Create(@"d:\TabTest.Encoded.xml"))
{
    xmlDoc.Save(xmlWriter);
}

XmlReader.Create, .

, , .

+1

, - . CDATA XML , .

+2

, XmlTextReader W3C. . ,

(# x20, #xD, #xA, # x9) (# x20) .

, , ( ), .

, XmlTextReader ( ), , , - &#x9;. .

+1

XmlReaderSettings.ComformanceLevel. , :

, XmlReader, Create, , XmlTextReader. , XmlTextReader, , Create

+1

All Articles