1

I have a class "WsReferenceValue" that contains a list of other class "Characteristic" ,and the class Characteristic has a property another class "Definition",this is my code:

  public class WsReferenceValue
  {
     [DataContract]
     public class WsReferenceValue : WsEntitiesDimension
     {
        public List<Characteristic> listCharacteristic { get; set; }       
     }
     [DataContract]
     public class Characteristic
     {      
          [DataMember]      
           public Definition definition { get; set; } 
     }
     [Serializable]   
     public class Definition
     {
          [XmlAttribute]
           public int id;
           [XmlAttribute]
           public string name;
      }
 }

My code:

 WsReferenceValue referenceV = new WsReferenceValue();
 List<Characteristic> ListFinalDynamic = new List<Characteristic>();
 foreach (var finalCharac in listeCharactresticFinal)
 {
                var lstChars = new Characteristic()
                        {
                            Definition = new Definition()
                            {
                                id = Dimension.ID,
                                name = Dimension.Name
                            }
                        };                          
             ListFinalDynamic.Add(lstChars);                    
            referenceV.listCharacteristic = ListFinalDynamic;    
}   

And I get this result:

 <WsReferenceValue>                     
    <listCharacteristic>
                <Characteristic>
                    <Definition>
                        <id>1</id>
                        <name>COMPANY</name>
                    </Definition>
                </Characteristic>

                <Characteristic>
                    <Definition>
                        <id>71</id>
                        <name>COUNTRY</name>
                    </Definition>
                </Characteristic>

                <Characteristic>
                    <Definition>
                        <id>45</id>
                        <name>CURRENCY</name>
                    </Definition>
                </Characteristic>
  </listCharacteristic>

And my goal is to get all list of Characteristic for one Reference with this format : (every Characteristic with attributes)

 <WsReferenceValue>                     
    <listCharacteristic>
                <Characteristic>
                    <Definition id=1 name="COMPANY" />                        
                </Characteristic>

                <Characteristic>
                    <Definition id=71 name="COUNTRY" />
                </Characteristic>

                <Characteristic>
                    <Definition id=45 name="CURRENCY" />                      
                </Characteristic>
  </listCharacteristic>

How can I fix it? Thanks

dbc
  • 104,963
  • 20
  • 228
  • 340
IngTun2018
  • 329
  • 1
  • 10
  • Refer this https://stackoverflow.com/questions/45270479/force-xmldocument-to-save-empty-elements-with-an-explicit-closing-tag – Jayakumar Thangavel Nov 13 '19 at 13:57
  • Thanks @JayakumarThangavel for your reply, but still not very clear ,because it is different from my objectif – IngTun2018 Nov 13 '19 at 14:12
  • How are you serializing to XML? Your c# data models have a mix of `XmlSerializer` attributes and data contract attributes so it isn't clear – dbc Nov 15 '19 at 04:59
  • @dbc I don't serialize,I didin't do anything for the serializing. The WCF runtime is serializing the results for me – IngTun2018 Nov 15 '19 at 14:22
  • 1
    Then you *probably* want [DataContract XML serialization and XML attributes](https://stackoverflow.com/q/4858798/3744182). WCF uses the data contract serializer by default which doesn't support XML attributes, so you need to switch to `XmlSerializer` by applying `[XmlSerializerFormat]` to your service or operations contract. If you were to share a [mcve] showing the service & operations contracts we could say for sure. – dbc Nov 15 '19 at 16:24
  • Thanks a lot @dbc, I understand very well now the confusion, Ok I will try your purpose,thanks – IngTun2018 Nov 15 '19 at 16:38
  • @IngTun2018 - did that work? If not can you share an updated [mcve]? – dbc Nov 19 '19 at 00:50
  • Thanks, @dbc , in fact I modify the concept of my problem: first I don't work with xmlserializer,I work only with DataContractSerializer so I delete Definition Class, and then I add list xelement into one xelement ,and I get my result – IngTun2018 Nov 20 '19 at 10:30

1 Answers1

0

In fact to resolve my problem: I modify the concept: first I don't work with XMLSerializer,I work only with DataContractSerializer so I delete Definition Class, and then I add list xelement into one xelement ,and I get my result,

My WsReferenceValue class:

  [DataContract]
 public class WsReferenceValue : WsEntitiesDimension
 { 
    [DataMember]
    public XElement listCharacteristic {get ; set;}
}

The implementation:

WsReferenceValue referenceV = new WsReferenceValue();
foreach (var finalCharac in listeCharactresticFinal)
{    
  var XelementTemp = new XElement("ReferenceValue",
      new XAttribute("Name", refVal.Name),
      new XAttribute("Id", refVal.ID)); 

      referenceV.listCharacteristic.Add(XelementTemp);                  
 }   

And I get my result exactly that I want it :

<listCharacteristic>
        <ReferenceValues xmlns="">
            <ReferenceValue Name="CC" Id="1"  />
            <ReferenceValue Name="S9999" Id="4" "0"/>
            <ReferenceValue Name="EE45" Id="5" />  
        </ReferenceValues>
</listCharacteristic>   
IngTun2018
  • 329
  • 1
  • 10