0

I don't understand why only the first "inner node" of the element gets deserialized into model.Description. How do I need to set up the Description node of the C# Body class so that all of the text between and gets assigned to the Description element?

Code:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var xDoc = XDocument.Load("xmlModel.xml");
            model.Body model = null;
            var xmlSerializer = new XmlSerializer(typeof(model.Body));
            using (var reader = xDoc.CreateReader() )
            {
                model = (model.Body)xmlSerializer.Deserialize(reader);
            }

            Console.Write(model.Description);

        }
        catch (System.Xml.XmlException e)
        {
            Trace.TraceWarning("XML Parse Error: xmlModel.xml, line " + e.LineNumber + ", position " + e.LinePosition + "\n" + e.Message);
        }
    }
}

Model:

namespace model
{
    [XmlRoot("body")]
    public class Body
    {
        [XmlElement("description")]
        public String Description { get; set; }

        [XmlElement("address")]
        public String UnitAddress { get; set; }
    }
}

XML:

<?xml version="1.0" encoding="utf-8" ?>
    <body>
        <description>
            <p>The PGT has 10 user-accessible 32-bit registers, which are used to configure,
operate, and monitor the state of the PGT.
</p>
<p>
  An IP bus write access to the PGT Control Register (PGT_CR) and the GPT Output Compare
  Register1 (PGT_OCR1) results in <i>one cycle of wait state</i><ph audience="internal"> (ips_xfr_wait high for 1 cycle)</ph>, while other valid IP bus accesses incur 0
  wait states.
</p>
<p>
  Irrespective of the Response Select <ph audience="internal">(resp_sel) </ph>signal
  value, a Write access to the PGT Status Registers (Read-only registers PGT_ICR1,
  PGT_ICR2, PGT_CNT) will generate a bus exception<ph audience="internal"> (ips_xfr_err signal will be asserted)</ph>.
</p>
<ul>
  <li>
    If the Response Select <ph audience="internal">(resp_sel) </ph>signal is driven Low, then the Read/Write access to the <i>unimplemented</i> address space of PGT (<i>ips_addr</i> is greater than or equal to $BASE + $028) will generate a bus exception<ph audience="internal"> (ips_xfr_err signal will be asserted)</ph>.
  </li>
  <li>
    If the Response Select <ph audience="internal">(resp_sel) </ph>is driven High, then the Read/Write access to the unimplemented address space of PGT will <i>not</i> generate any error response (like a bus exception).
  </li>
</ul>
    </description>
    <address>0x0000</address>
</body>

Thanks a ton, Chris

  • When you say "first inner node" what do you mean? IE: In your testing, what is getting assigned to Description? – Vlad274 Feb 10 '15 at 21:56
  • Description has no inner text in both occurrences that are shown in your sample xml. – Keith Payne Feb 10 '15 at 21:57
  • I see that there is only one, actually. Regardless, it doesn't contain any inner text, only child nodes. – Keith Payne Feb 10 '15 at 21:59
  • I'm assuming that you're looking to have the HTML content be stored as a singular string represented as "Description"? If that's correct, I believe you'll need to wrap the whole block in a CDATA section or properly encode the HTML - see http://stackoverflow.com/q/1398571/109456 – Matt Klinker Feb 10 '15 at 22:28

1 Answers1

0

Please add the <description><![CDATA[... ]]></description> around your description data.

Then you will be able to extract the entire content with model.Description

JunaidKirkire
  • 878
  • 1
  • 7
  • 17