Click here to Skip to main content
15,039,714 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the following Xml I want to deserialize to an object:

XML
<?xml version="1.0" encoding="utf-8"?>
<ExternalEntry>
  <Header>
    <EntryType>Question</EntryType>
    <Source>
      <Name>Testing</Name>
      <SourceIdentifier>48</SourceIdentifier>
    </Source>
  <TransactionId></TransactionId>
  <EntryBody>
    <Question>How are you?</Question>
    <Answer>Fine</Answer>
  </EntryBody>
</ExternalEntry>


It all works fine except for the Entry Body contents I want brought into either a string or an XmlElement type. The following class does the Header just fine, but the EntryBody comes out as "How are you?". What I want it to come back as is
XML
<Question>How are you?</Question><Answer>Fine</Answer>


How can I get the result above as a string?

C#
 /// <summary>
/// The root element of an ProgressNote.
/// </summary>
[XmlRoot("ExternalEntry")]
public class ExternalEntryData
{
    /// <summary>
    /// Gets or sets the HeaderData object.
    /// </summary>
    [XmlElement("Header")]
    public HeaderData HeaderData { get; set; }

    /// <summary>
    /// Gets or sets the EntryBody string.
    /// </summary>
    [XmlElement("EntryBody")]
    public string EntryBody { get; set; }
}
Posted

1 solution

EntryBody is declared as a string property of your class. Are you actually putting XML into the EntryBody property? I'd recommend making EntryBody into a separate type with its own Question and Answer properties, much as you did with the HeaderData.

P.S. in your original XML, the Header tag is never closed! Perhaps this is breaking something in your code?
   
Comments
Tim Groven 9-May-12 12:37pm
   
The close tag for header was a typo. I could make EntryBody a separate type, but EntryBody could have different elements and attributes depending on the EntryType in the header. This was why I was trying to just get the Xml in a string so I could send it out after finding out the type.
ekolis 9-May-12 13:07pm
   
Hmm... well, you could always make EntryBody an abstract type...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900