简体   繁体   中英

How to change the output of an XML in WCF

I defined these classes:

 [DataContract(Name = "cardTemplateDefinition", Namespace = "")]
    public class CardTemplateDefinition
    {
      [DataMember(IsRequired = false, Name = "riskInfoList")]
        public RiskInfoList RiskInfoList { get; set; }
    }

    [DataContract(Namespace = "")]
    public class RiskInfoList
    {
        [DataMember(IsRequired = false, Name = "riskCount")]
        public string RiskCount { get; set; }

        [DataMember(IsRequired = false, Name = "riskInfo")]
        public List<RiskInfo> RiskInfo { get; set; }
    }

    [DataContract(Namespace = "")]
    public class RiskInfo
    {
        [DataMember(IsRequired = false, Name = "riskType")]
        public string RiskType { get; set; }

        [DataMember(IsRequired = false, Name = "riskDescription")]
        public string RiskDescription { get; set; }

        [DataMember(IsRequired = false, Name = "autoRisk")]
        public AutoRiskEntity AutoRisk { get; set; }

        [DataMember(IsRequired = false, Name = "dwellingRisk")]
        public DwellingRiskEntity DwellingRisk { get; set; }
    }

    [DataContract(Namespace = "")]
    public class DwellingRiskEntity
    {
        [DataMember(IsRequired = false, Name = "location")]
        public string Location { get; set; }
    }

    [DataContract(Namespace = "")]
    public class AutoRiskEntity
    {
        [DataMember(IsRequired = false, Name = "vin")]
        public string Vin { get; set; }

        [DataMember(IsRequired = false, Name = "make")]
        public string Make { get; set; }

        [DataMember(IsRequired = false, Name = "model")]
        public string Model { get; set; }

        [DataMember(IsRequired = false, Name = "licensePlate")]
        public string LicensePlate { get; set; }

        [DataMember(IsRequired = false, Name = "year")]
        public string Year { get; set; }
    }

This is creating the following XML

<riskInfoList>
   <riskCount></riskCount>
   <riskInfo>
      <!--Zero or more repetitions:-->
      <riskInfo>
         <autoRisk>
            <licensePlate>?</licensePlate>
             <make>?</make>
            <model>?</model>
            <vin>?</vin>
            <year>?</year>
         </autoRisk>
         <dwellingRisk>
            <location>?</location>
         </dwellingRisk>
         <riskDescription>?</riskDescription>
         <riskType>?</riskType>
      </riskInfo>
   </riskInfo>
</riskInfoList>

But what I want is:

  <riskInfoList>
     <riskCount>?</riskCount>
          <!--Zero or more repetitions:-->
          <riskInfo>
             <autoRisk>
                <licensePlate>?</licensePlate>
                 <make>?</make>
                <model>?</model>
                <vin>?</vin>
                <year>?</year>
             </autoRisk>
             <dwellingRisk>
                <location>?</location>
             </dwellingRisk>
             <riskDescription>?</riskDescription>
             <riskType>?</riskType>
          </riskInfo>
    </riskInfoList>

I want to get rid of the riskInfo duplicate root. I tried to change the field definition by eliminating the RiskInfoList class and change the RiskInfoList field in the carddefinition class to be directly a list of RiskInfo. This rendered the xml as I wanted but then I cannot included the riskCount element becuase it needs to be at the riskinfolist level.

How can I remediate this?

The DataContractSerializer does not support what you're wanting to do, but you can switch to use the XmlSerializer and achieve the outcome you're looking for:

First, update your ServiceContract to also have the XmlSerializerFormat attribute:

[ServiceContract, XmlSerializerFormat]
public interface IService1
{
    [OperationContract]
    CardTemplateDefinition SomeMethod(CardTemplateDefinition composite);

}

Then remove the [DataContract] attributes from your model classes and change the [DataContract] attributes for [XmlElement] attributes:

public class CardTemplateDefinition
{
    [XmlElement("riskInfoList")]      
    public RiskInfoList RiskInfoList { get; set; }
}

public class RiskInfoList 
{
    [XmlElement("riskCount")]
    public string RiskCount { get; set; }

    [XmlElement("riskInfo")]
    public List<RiskInfo> RiskInfo { get; set; }
}

public class RiskInfo
{
    [XmlElement("riskType")]
    public string RiskType { get; set; }

    [XmlElement("riskDescription")]
    public string RiskDescription { get; set; }

    [XmlElement("autoRisk")]
    public AutoRiskEntity AutoRisk { get; set; }

    [XmlElement("dwellingRisk")]
    public DwellingRiskEntity DwellingRisk { get; set; }
}


public class DwellingRiskEntity
{
    [XmlElement("location")]        
    public string Location { get; set; }
}

public class AutoRiskEntity
{
    [XmlElement("vin")]
    public string Vin { get; set; }

    [XmlElement("make")]
    public string Make { get; set; }

    [XmlElement("model")]
    public string Model { get; set; }

    [XmlElement("licensePlate")]
    public string LicensePlate { get; set; }

    [XmlElement("year")]
    public string Year { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM