Click here to Skip to main content
15,881,568 members
Home / Discussions / C#
   

C#

 
GeneralRe: convert 20171005 to 10/05/2017 Pin
jkirkerx9-Oct-17 7:05
professionaljkirkerx9-Oct-17 7:05 
GeneralRe: convert 20171005 to 10/05/2017 Pin
Richard Deeming9-Oct-17 8:00
mveRichard Deeming9-Oct-17 8:00 
AnswerRe: convert 20171005 to 10/05/2017 Pin
Gerry Schmitz7-Oct-17 6:08
mveGerry Schmitz7-Oct-17 6:08 
GeneralRe: convert 20171005 to 10/05/2017 Pin
jkirkerx9-Oct-17 7:07
professionaljkirkerx9-Oct-17 7:07 
QuestionSerializing \r\n read issue Pin
C. David Johnson6-Oct-17 6:07
C. David Johnson6-Oct-17 6:07 
AnswerRe: Serializing \r\n read issue Pin
jschell6-Oct-17 7:03
jschell6-Oct-17 7:03 
GeneralRe: Serializing \r\n read issue Pin
C. David Johnson6-Oct-17 7:26
C. David Johnson6-Oct-17 7:26 
GeneralRe: Serializing \r\n read issue Pin
Richard Deeming6-Oct-17 7:46
mveRichard Deeming6-Oct-17 7:46 
XML and white-space don't generally work well together:
2.10 White Space Handling | Extensible Markup Language (XML) 1.1[^]

Unfortunately, the usual workarounds don't seem to apply to XML deserialization. Adding xml:space="preserve", or passing in an XmlReader set to not ignore white-space, makes no difference to the result.

The only way I can see for you to preserve the white-space in an empty node is to use a CDATA element:
XML
<KEY FIELDID="1"><![CDATA[
]]></KEY>

C#
[Serializable]
[XmlType("KEY")]
public class KeyType
{
    [XmlAttribute("FIELDID")]
    public int FieldId { set; get; }
    
    [XmlText]
    public string Value { set; get; }
    
    [XmlElement("")]
    public XmlCDataSection Content
    {
        get
        {
            return new XmlDocument().CreateCDataSection(Value);
        }
        set
        {
            if (value == null)
            {
                Value = null;
            }
            else
            {
                Value = value.Value;
            }
        }
    }
}

c# - How do you serialize a string as CDATA using XmlSerializer? - Stack Overflow[^]

NB: ALL white-space within the CDATA element will be returned. So if your XML is:
XML
<DATA>
    <KEY FIELDID="1"><![CDATA[
    ]]></KEY>
</DATA>
your content will include the white-space at the start of the next line. If you wanted just the line break, the closing ]]> would need to be the first thing on the line, with no indentation.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Serializing \r\n read issue Pin
C. David Johnson6-Oct-17 8:05
C. David Johnson6-Oct-17 8:05 
GeneralRe: Serializing \r\n read issue Pin
Richard Deeming6-Oct-17 8:07
mveRichard Deeming6-Oct-17 8:07 
GeneralRe: Serializing \r\n read issue Pin
jschell11-Oct-17 5:53
jschell11-Oct-17 5:53 
AnswerRe: Serializing \r\n read issue Pin
Gerry Schmitz7-Oct-17 6:02
mveGerry Schmitz7-Oct-17 6:02 
QuestionDecimal compare Pin
VK196-Oct-17 4:16
VK196-Oct-17 4:16 
AnswerRe: Decimal compare Pin
Richard Deeming6-Oct-17 4:49
mveRichard Deeming6-Oct-17 4:49 
SuggestionRe: Decimal compare Pin
Richard MacCutchan6-Oct-17 4:53
mveRichard MacCutchan6-Oct-17 4:53 
GeneralRe: Decimal compare Pin
Mycroft Holmes7-Oct-17 14:38
professionalMycroft Holmes7-Oct-17 14:38 
AnswerRe: Decimal compare Pin
Luc Pattyn6-Oct-17 4:53
sitebuilderLuc Pattyn6-Oct-17 4:53 
AnswerRe: Decimal compare Pin
C. David Johnson6-Oct-17 5:56
C. David Johnson6-Oct-17 5:56 
GeneralRe: Decimal compare Pin
Luc Pattyn6-Oct-17 6:06
sitebuilderLuc Pattyn6-Oct-17 6:06 
GeneralRe: Decimal compare Pin
C. David Johnson6-Oct-17 6:13
C. David Johnson6-Oct-17 6:13 
AnswerRe: Decimal compare Pin
Gerry Schmitz7-Oct-17 5:48
mveGerry Schmitz7-Oct-17 5:48 
QuestionDeserialization issue (Should be easy) Pin
C. David Johnson5-Oct-17 9:38
C. David Johnson5-Oct-17 9:38 
AnswerRe: Deserialization issue (Should be easy) Pin
Richard Deeming6-Oct-17 2:25
mveRichard Deeming6-Oct-17 2:25 
AnswerRe: Deserialization issue (Should be easy) Pin
C. David Johnson6-Oct-17 5:03
C. David Johnson6-Oct-17 5:03 
QuestionHeisenbug in program without async, etc. Pin
Alexander Kindel5-Oct-17 6:47
Alexander Kindel5-Oct-17 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.