2016-06-08 5 views
1

Ich raubend einen Web-API, und ich bekomme ein XML-Ergebnis:C# XML-Deserialisierung Element in Objekteigenschaft

<tarification cle="XXXX"> 
     <gamme reference="refX"> 
      <tarif formula="F100">44.84</tarif> 
      <tarif formula="F125">47.63</tarif> 
      <tarif formula="F150">57.34</tarif> 
      <tarif formula="F200">67.95</tarif> 
      <option name="indiv-acc">0.5</option> 
      <option name="rap-cor">6.06</option> 
     </gamme> 
    </tarification> 

ich dieses Modell verwenden, um deserialisiert:

[XmlRoot(ElementName = "tarification", Namespace="")] 
    public class TarifResponse 
    { 
     [XmlElement(ElementName = "gamme")] 
     public Gamme Gamme { get; set; } 
    } 

    public class Gamme 
    { 
     [XmlAttribute(AttributeName="reference")] 
     public string Name { get; set; } 

     [XmlElement(ElementName = "tarif")] 
     public Formula[] Formulas { get; set; } 

     [XmlElement(ElementName = "option")] 
     public Option[] Options { get; set; } 

    } 

    public class Formula 
    { 
     [XmlAttribute(AttributeName="formula")] 
     public string Name { get; set; } 

     // WRONG ATTRIBUTE.. but witch one ? 
     [XmlElement] 
     public Decimal Amount { get; set; } 
    } 

    public class Option 
    { 
     [XmlAttribute(AttributeName = "name")] 
     public string Name { get; set; } 

     // WRONG ATTRIBUTE.. but witch one ? 
     [XmlElement] 
     public Decimal Amount { get; set; } 
    } 

Das TarifResponse Objekt ist erstellt, sind alle Felder gefüllt, mit Ausnahme der beiden Mengenfelder. Ich erwarte, dass dies, weil die richtige xml sein sollte:

<amount>5.5</amount> 

innerhalb tarif oder Optionselemente ..

Ist das Format deserializable ist? Gibt es einen Weg dazu mit Attribut? Ist das sogar ein akzeptables XML-Format?

danke

Antwort

0

Das [XmlText] Attribut wird den inneren Text des Knotens erhalten. Möglicherweise müssen Sie [XmlText(Type=typeof(decimal))] verwenden, um die analysierte Nummer zu erhalten.

public class Option 
{ 
    [XmlAttribute(AttributeName = "name")] 
    public string Name { get; set; } 

    [XmlText] 
    public Decimal Amount { get; set; } 
} 
+0

Danke, es war das Attribut, das ich suchte. – BAXOM