2017-08-25 2 views
0

Je fais un projet C# et j'ai un objet codé en XML; une instance d'exemple serait:Comment désérialiser correctement les attributs et les tableaux XML?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Entity Type="StartRunTestSetResponse"> 
    <Fields> 
     <Field Name="SuccessStaus"> 
      <Value>2</Value> 
     </Field> 
     <Field Name="info"> 
      <Value></Value> 
     </Field> 
    </Fields> 
</Entity> 

J'ai besoin les informations d'attributs, car il est une nécessité dans les paires valeur-clé de l'objet a.

Ma grammaire désérialisation ressemble à ceci:

[DataContract(Name="Entity", Namespace="")] 
[XmlSerializerFormat] 
[KnownType(typeof(SRTSRField))] 
[KnownType(typeof(SRTSRValue))] 
public class StartRunTestSetResponse 
{ 
    [DataMember(Name="Type"), XmlAttribute("Type")] 
    public string type { get; set; } 

    [DataMember(Name = "Fields", IsRequired = true), XmlElement("Fields")] 
    public List<SRTSRField> fields { get; set; } 

    internal StartRunTestSetResponse() { fields = new List<SRTSRField>(); } 
} 
[DataContract(Name = "Field", Namespace = "")] 
[KnownType(typeof(SRTSRValue))] 
public class SRTSRField 
{ 
    [DataMember(Name = "Name"), XmlAttribute("Name")] 
    public string name {get; set;} 

    [DataMember(Name = "Value"), XmlElement("Value")] 
    public SRTSRValue value { get; set; } 
} 
[DataContract(Name = "Value", Namespace = "")] 
public class SRTSRValue 
{ 
    [DataMember, XmlText] 
    public string value { get; set; } 
} 

Maintenant, cela ne fonctionne pas; au moment où il analyse à l'élément Fields et puis tout enfant de celui-ci est null.

Répondre

1

Vous pouvez simplifier votre modèle

public class Entity 
{ 
    [XmlAttribute] 
    public string Type { get; set; } 
    [XmlArrayItem("Field")] 
    public Field[] Fields { get; set; } 
} 

public class Field 
{ 
    [XmlAttribute] 
    public string Name { get; set; } 
    public string Value { get; set; } 
} 

Alors désérialisation serait

XmlSerializer ser = new XmlSerializer(typeof(Entity)); 
using (StringReader sr = new StringReader(xmlstring)) 
{ 
    var entity = (Entity)ser.Deserialize(sr); 

} 
+0

Eh bien, je n'utilise plus jamais DataContract for XML! – Olivier10178

0
[XmlRoot(ElementName="Field")] 
public class Field { 
    [XmlElement(ElementName="Value")] 
    public string Value { get; set; } 
    [XmlAttribute(AttributeName="Name")] 
    public string Name { get; set; } 
} 

[XmlRoot(ElementName="Fields")] 
public class Fields { 
    [XmlElement(ElementName="Field")] 
    public List<Field> Field { get; set; } 
} 

[XmlRoot(ElementName="Entity")] 
public class Entity { 
    [XmlElement(ElementName="Fields")] 
    public Fields Fields { get; set; } 
    [XmlAttribute(AttributeName="Type")] 
    public string Type { get; set; } 
} 

Createdy par: http://xmltocsharp.azurewebsites.net/ Il est vraiment utile

0

Je voudrais créer un dictionnaire à l'aide xml LINQ .

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication74 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      Dictionary<string,int?> dict1 = doc.Descendants("Field") 
       .GroupBy(x => (string)x.Attribute("Name"), y => string.IsNullOrEmpty((string)y.Element("Value")) ? null : (int?)y.Element("Value")) 
       .ToDictionary(x => x.Key, y => y.FirstOrDefault()); 


     } 

    } 



}