为什么不对整个<description>节点进行反序列化?</description>

时间:2015-02-10 21:44:38

标签: c# xml linq serialization

我不明白为什么只有第一个&#34;内部节点&#34;将元素反序列化为model.Description。如何设置C#Body类的Description节点,以便将之间的所有文本分配给Description元素?

代码:

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);
        }
    }
}

型号:

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>

非常感谢, 克里斯

1 个答案:

答案 0 :(得分:0)

请在说明数据周围添加<description><![CDATA[... ]]></description>

然后,您将能够使用model.Description

提取整个内容
相关问题