【问题标题】:XMLSerialization NestingXML序列化嵌套
【发布时间】:2015-03-19 21:02:03
【问题描述】:

我正在做一些 XML 序列化并尝试像这样获得输出:

<Claim>
   <Source>...</Source>
   <Vehicle>...</Vehicle>
   <ThirdParty>...</ThirdParty>
   <ThirdParty>...</ThirdParty>
   <ThirdParty>...</ThirdParty>
</Claim>

但是我的输出是:

  <Claim>
       <Source>...</Source>
       <Vehicle>...</Vehicle>
       <ThirdParty>
          <ThirdParty>...</ThirdParty>
          <ThirdParty>...</ThirdParty>
          <ThirdParty>...</ThirdParty>
       </ThirdParty>
    </Claim>

第三方嵌套在列表中而不是声明中,我怎样才能在 XML 的基础上而不是在另一个第三方节点中获取第三方。我希望这是有道理的。

我正在尝试序列化的对象:

    public class ThirdParty
    {
        public ThirdPartyDetails Details { get; set; }
        public ThirdPartyVehicle Vehicle { get; set; }
        public ThirdPartyPolicy Policy { get; set; }
        public ThirdPartyPrincipleCompany PrincipleCompany { get; set; } 
    }


public class Claim
{
     public List<ThirdParty> ThirdParty { get; set; }
}

初始化

ThirdParty ThirdParty1 = new ThirdParty{ Details = ThirdPartyDetails, Policy=ThirdPartyPolicy, PrincipleCompany=ThirdPartyPrinciple, Vehicle=ThirdPartyVehicle};

ThirdParty ThirdParty2 = new ThirdParty{ Details = ThirdPartyDetails, Policy=ThirdPartyPolicy, PrincipleCompany=ThirdPartyPrinciple, Vehicle=ThirdPartyVehicle};

List<ThirdParty> LP = new List<ThirdParty>();

LP.Add(ThirdParty1);
LP.Add(ThirdParty2);


Claim Claim = new Claim { ThirdParty = LP };

序列化

  var subReq = Claim;
            using (StringWriter sww = new Utf8StringWriter())
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
                {
                    Indent = true,
                    OmitXmlDeclaration = false,
                    Encoding = Encoding.Unicode
                };

                using (XmlWriter writer = XmlWriter.Create(sww, xmlWriterSettings))
                {
                    xsSubmit.Serialize(writer, subReq);
                    var xml = sww.ToString();
                    PrintOutput(xml);

                    Console.WriteLine(xml);
                }
            }

编辑: 回应彼得:

好的,现在我的输出有问题,我没有使用它 我认为正确。所以因为我继承了 List 然后我可以使用 List 的 Add 方法: 索赔。添加(第三方 1);声明。添加(第三方 2);现在我的输出是 ... ……完全 忽略的来源和车辆。有什么想法吗?

【问题讨论】:

    标签: c# xmlserializer


    【解决方案1】:

    您已声明 Claim 有一个 ThirdParty 的集合,但您希望它像 Claim 是一个 ThirdParty 的集合一样序列化。

    要获得 Claim 第三方集合的场景,请从 List&lt;ThirdParty&gt; 派生 Claim 并添加所需的属性和方法。

    此示例假设您有另一个类 Vehicle。

    public class Claim : List<ThirdParty> {
      public string Source { get; set; }
      public Vehicle Vehicle { get; set; }
    }
    

    Claim 是 ThirdParty 的列表,但还有其他属性和潜在的其他方法。

    然而

    那行不通。如果你这样做,它会停止序列化所有其他属性,你得到的只是集合的孩子。

    所以坚持组合,但用[XmlElement] 属性标记它,像这样:

    public class Claim 
    {
      public Source Source { get; set; }
      public Driver Driver { get; set; }
      public Owner Owner { get; set; }
      public Vehicle Vehicle { get; set; }
      public Accident Accident { get; set; }
      public Policy Policy { get; set; }
      public Insurer Insurer { get; set; }
      public Solicitor Solicitor { get; set; }
      [XmlElement]
      public List<ThirdParty> ThirdParty { get; set; } 
    }
    

    【讨论】:

    • 嗨,彼得,感谢您的回复。这是有道理的,但是 XML 文档中还有其他节点,例如 SourceVehicle(请参阅我的问题中编辑的 XML 示例)。你的方法还适用吗?
    • 短版:是的。长版:见扩展示例。这基本上是一个继承与组合的问题。您想要表达的对象图意味着继承(是一个列表
    • 好的,现在我的输出有问题,我认为我没有正确使用它。所以因为我继承了List&lt;ThirdParty&gt; 的属性,所以我可以使用List&lt;T&gt; 的Add 方法:claim.Add(ThirdParty1); claim.Add(ThirdParty2); 现在我的输出是&lt;ArrayOfThirdParty&gt; &lt;ThirdParty&gt;...&lt;/ThirdParty&gt; &lt;ThirdParty&gt;...&lt;/ThirdParty&gt; &lt;/ArrayOfThirdParty&gt; 它完全忽略了Source 和Vehicle。有什么想法吗?
    • Claim的类定义中添加[XmlRoot(ElementName="Claim")],就可以正确序列化了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多