XML序列化帮助 [英] XML Serialization Help

查看:84
本文介绍了XML序列化帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要反序列化为对象的Xml:

I have the following Xml I want to deserialize to an object:

<?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>



除了我要带入字符串或XmlElement类型的Entry Body内容之外,其他所有内容都工作正常.下面的类可以使Header正常运行,但是EntryBody的显示形式为你好吗?".我希望它恢复原状



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

<Question>How are you?</Question><Answer>Fine</Answer>



如何以字符串形式获取上面的结果?



How can I get the result above as a string?

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

推荐答案

EntryBody被声明为您的类的字符串属性.您实际上是将XML放入EntryBody属性中吗?我建议将EntryBody设置为具有自己的Question和Answer属性的单独类型,就像使用HeaderData一样.

P.S.在原始XML中,Header标记永远不会关闭!也许这会破坏代码中的某些内容?
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?


这篇关于XML序列化帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆