开发者

XmlTextReader node value not the same as value set

开发者 https://www.devze.com 2023-02-02 18:58 出处:网络
I\'ve a XmlTextReader initialized by a mem开发者_C百科orystream, and the value in MemoryStream is :

I've a XmlTextReader initialized by a mem开发者_C百科orystream, and the value in MemoryStream is :

<val><![CDATA[value]]></val>

In MemoryStream I've the good byte array corresponding to this value, but when I do :

XmlTextReader reader = new XmlTextReader(myMemoryStream);
reader.ReadToFollowing("val");
string result = reader.ReadElementContentAsString();

I get the following result value :

"\r\n\t\t\t\tvalue\r\n\t\t\t"

Why carriage returns and tabulations are appened to the value? I don't add it when I create the reader...

Hope I'm clear enough.

Thanks for help.

[EDIT]

    byte[] DEBUGvalue = myMemoryStream.GetBuffer()
                                      .SkipWhile((b) => b != (byte)'[')
                                      .TakeWhile((b) => b != (byte)']')
                                      .Select((b) => b).ToArray();

And DEBUGvalue contains :

    [0] 91  byte ([)
    [1] 67  byte (C)
    [2] 68  byte (D)
    [3] 65  byte (A)
    [4] 84  byte (T)
    [5] 65  byte (A)
    [6] 91  byte ([)
    [7] 118 byte (v)
    [8] 97  byte (a)
    [9] 108 byte (l)
    [10]    117 byte (u)
    [11]    101 byte (e)
    [12]    32  byte ( )
    [13]    32  byte ( )
    [14]    32  byte ( )
    [15]    32  byte ( )
    [16]    32  byte ( )


Are you sure this is the literal input for this result?
Have you tried dumping the memStream to a (debug) file and examen the contents?

ReadElementContentAsString() will concatenate CDATA and whitespace. It looks like your input is more like

<val>
    <![CDATA[value]]>
    </val>


You could create your XmlReader like this.

 var settings = new XmlReaderSettings { IgnoreWhitespace = true };
 var reader = XmlReader.Create(new StringReader(@"<val>       <![CDATA[value]]>       </val>"), settings);

That would make things a bit easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消