Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7766401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:24:26+00:00 2026-06-01T15:24:26+00:00

I’m pulling my hair out on this. I do some manual deserialization using XmlReader

  • 0

I’m pulling my hair out on this. I do some manual deserialization using XmlReader – nothing serious, done that zilion times. But this is something I can’t figure out.

This is sample xml file

<?xml version="1.0" encoding="utf-8"?>
<Theme name="something" version="1.0.0.0">
  <Thumbnail length="1102">[some base64 encoded data]
</Thumbnail>
  <Backgrounds>
    <string>Themes\something\Backgrounds\file1</string>
    <string>Themes\something\Backgrounds\file2</string>
    <string>Themes\something\Backgrounds\file3</string>
  </Backgrounds>
  <Stickers>
    <string>Themes\something\Stickers\stick1</string>
    <string>Themes\something\Stickers\stick1</string>
    <string>Themes\something\Stickers\stick1</string>
  </Stickers>
  <PreviewImages>
    <string>Themes\something\Preview\rh_01.jpg</string>
    <string>Themes\something\Preview\rh_02.jpg</string>
    <string>Themes\something\Preview\rh_03.jpg</string>
  </PreviewImages>
</Theme>

This is deserialization code (a bit simplified):

public void ReadXml(System.Xml.XmlReader reader)
{       
    /* Read attributes - not important here */

    while (reader.Read())
    {
        Console.WriteLine("Main: {0} {1}", reader.NodeType, reader.Name);
        switch (reader.Name)
        {
            case Xml.Elements.Thumbnail:
                this._thumbnail = Xml.DeserializeBitmap(reader);
                Console.WriteLine("Inner: {0} {1}", reader.NodeType, reader.Name);
                break;
            case Xml.Elements.Backgrounds:
                this._backgrounds = Xml.DeserializeListOfStrings(reader);
                break;
            case Xml.Elements.Stickers:
                this._stickers = Xml.DeserializeListOfStrings(reader);
                break;
            case Xml.Elements.PreviewImages:
                this._previewImages = Xml.DeserializeListOfStrings(reader);
                break;
        }

        if (reader.NodeType == System.Xml.XmlNodeType.EndElement
                && reader.Name == Xml.Root)
            break;
    }
}

The problem:

After this._thumbnail is deserialized, the reader is positioned on closing element of Thumbnail node. Then reader.Read() at the beginning of while loop is called… and the reader gets positioned on starting element of a string node. The Backgrounds element is skipped! Why?

This happens when the reader is the XmlTextReader and it’s WhitespaceHandling property is set to WhitespaceHandling.None or WhitespaceHandling.Significant.

If it is set to WhitespaceHandling.All everything works as expected. After calling reader.Read() the reader is positioned on starting element of Backgrounds node.


[EDIT] I’ve added two debug lines to the example code.

With WhitespaceHandling.All I get this:

Main: Whitespace 
Main: Element Thumbnail
Inner: EndElement Thumbnail
Main: Element Backgrounds
Main: Whitespace 
Main: Element Stickers
Main: Whitespace 
Main: Element PreviewImages
Main: Whitespace 
Main: EndElement Theme

With WhitespaceHandling.Significant I get this:

Main: Element Thumbnail
Inner: EndElement Thumbnail
Main: Element string
Main: Text 
Main: EndElement string
Main: Element string
Main: Text 
Main: EndElement string
Main: Element string
Main: Text 
Main: EndElement string
Main: EndElement Backgrounds

[EDIT 2] Adjusted debug output a bit to be more readable.

As you can see, the debug output for WhitespaceHandling.Significant ends on </Backgrounds>. That’s because my Xml.DeserializeListOfStrings does not yet check if it’s positioned correctly and “accidentally” reads document to the end. But that’s not the scope of this question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:24:28+00:00Added an answer on June 1, 2026 at 3:24 pm

    The cause of my headache is XmlReader.ReadElementContentAsBase64 method that I use to deserialize <Thumbnail> node. I was experimenting with it in a loop:

    private static byte[] ReadBytes(System.Xml.XmlReader reader)
    {
        byte[] buffer = new byte[128];
        int length = XmlConvert.ToInt32(reader[Xml.Attributes.Length]);
    
        using (MemoryStream ms = new MemoryStream(length))
        {
            int count = 0;
    
            do
            {
                count = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length);
                ms.Write(buffer, 0, count);
    
            } while (ms.Length < length);
    
            return ms.GetBuffer();
        }
    }
    

    However MSDN says that:

    If the count value is higher than the number of bytes in the document, or if it is equal to the number of bytes in the document, the XmlNodeReader reads all the remaining bytes in the document and returns the number of bytes read. The next ReadElementContentAsBase64 method call returns a zero and moves the reader to the node following the EndElement node.

    If you call Read before all of the element content is consumed, the reader may behave as if the first content was consumed and then the Read method was called. This means that the reader will read all the text until the end element is encountered. It will then read the end tag node, read the next node, and then position itself on the next subsequent node.

    It seems that despite reading to the end of element’s content (I know data length so theoretically I can do that), the XmlReader did not consider that I’ve “consumed” all of the element’s content. That caused some unexpected behaviour described in MSDN.

    The XmlReader behaved the same with WhietespaceHandling.All and WhietespaceHandling.Significant. My code worked with WhietespaceHandling.All because after last call to XmlReader.ReadElementContentAsBase64, the reader was skipping non significant whitespace. If source xml file would contain no newlines and tabs, my code would fail with WhietespaceHandling.All too.

    The solution is to modify while loop to make one additional call to XmlReader.ReadElementContentAsBase64 after all bytes are red. The downside of this approach is that after that additional call the reader is moved to the node following the EndElement node.

    do
    {
        count = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length);
        if (count > 0)
            ms.Write(buffer, 0, count);
    
    } while (count > 0);
    

    One could also use XmlTextReader.ReadBase64 method to read whole element content at once, but I’m forced to use only XmlReader base as my class implements IXmlSerializable, so this method is not available for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.