Sign in to follow this  

[.net] Errors Writing XML File

This topic is 3868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a little app that will assist me with my XNA programming, but I'm having this one last gotcha in my code. I'm trying to write out the following XML:
<CharacterRegions>
      <CharacterRegion>
<!--- no spaces in real code, just for forum --->
        <Start>& #32;</Start>
        <End>& #126;</End>
      </CharacterRegion>
    </CharacterRegions>





And this is how I'm doing it:
writer.WriteWhitespace("\n\t\t");
writer.WriteStartElement("CharacterRegions");
writer.WriteWhitespace("\n\t\t\t");
writer.WriteStartElement("CharacterRegion");
writer.WriteWhitespace("\n\t\t\t\t");
writer.WriteElementString("Start", "& #32;"); //no space in actual code, just for forum
writer.WriteWhitespace("\n\t\t\t\t");
writer.WriteElementString("End", "& #126;"); //no space in actual code, just for forum
writer.WriteWhitespace("\n\t\t\t");
writer.WriteEndElement();
writer.WriteWhitespace("\n\t\t");
writer.WriteEndElement();




But when I try to write the "&" it converts it to "& amp;" (without the space) which as I understand is how to represent the & in HTML or XML. But how can I get the XmlWriter to not convert the &?

Share this post


Link to post
Share on other sites
Why would you not want it converting, as when you read it back in, it would no longer be & and I guess it would generally be a malformed xml.

And why not use XmlDocument ? is there any real need to have that whitespace bloat ? :)

Share this post


Link to post
Share on other sites
XmlWriter assumes that the string you give it is not yet xml-safe. So it encodes it.

Basically, I'd suggest on of the two:
1) Don't pass the character code, pass the character it self - XmlWriter whill encode it, if necessary;
2) Pass the character code as an integer - simply save integer numbers, instead of character codes (number to char or char to number conversion is easy in all programming languages).

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
Actually, not VB

I know nothing about the VB (I never coded in VB), but CChar and Val seems to be doing exactly what I was talking about... I could be wrong, though.

Quote:
Original post by capn_midnight
or javascript.

As for javascript, I think string.charCodeAt() and string.fromCharCode() should work. Again, I didn't test anything, so I might be wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Paulius Maruska
Quote:
Original post by capn_midnight
Actually, not VB

I know nothing about the VB (I never coded in VB), but CChar and Val seems to be doing exactly what I was talking about... I could be wrong, though.

Quote:
Original post by capn_midnight
or javascript.

As for javascript, I think string.charCodeAt() and string.fromCharCode() should work. Again, I didn't test anything, so I might be wrong.


I know of these functions and A) they are not immediately obvious to find, and B) they are quite finnicky to use.

Share this post


Link to post
Share on other sites

This topic is 3868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this