2016-04-27 1 views
0

Hallo, ich versuche, eine XML-Datei in Objekt zu deserialisieren, aber ich bekomme keine Werte.Ich denke, es gibt ein Problem mit den Klassenmodellen. Wenn ich es ausführe, bekomme ich keine Daten in das resultierende Objekt gefüllt.Keine Daten beim Deserialisieren von XML zu Objekt in C#

XDocument test = XDocument.Load(UrlAdrress); 
var result = XMLHelper.FromXML<XmlSportsModel>(test); 

Die Struktur ist richtig enter image description here

Hier meine xml aussieht und ich kann es nicht ändern. XMLFile

Mein Deserializer

public static class XMLHelper 
{ 
    public static T FromXML<T>(XDocument data) 
    { 
     var xmlSerializer = new XmlSerializer(typeof(T)); 
     using (var reader = data.CreateReader()) 
     { 
      return (T)xmlSerializer.Deserialize(reader); 
     }    
    } 
} 

Meine Modelle

[XmlRoot("XmlSports")] 
public class XmlSportsModel 
{ 
    [XmlElement("Sport")] 
    public SportModel[] Sports { get; set; }  
} 

    public class SportModel 
{ 
    [XmlElement(ElementName = "Name")] 
    public string Name { get; set; } 

    [XmlElement(ElementName = "ID")] 
    public string Id { get; set; } 
    [XmlElement(ElementName = "Event")] 
    public EventModel[] Events { get; set; } 
} 


    public class EventModel 
{ 
    [XmlElement("Name")] 
    public string Name { get; set; } 
    [XmlElement("ID")] 
    public int Id { get; set; } 
    [XmlElement("CategoryID")] 
    public int CategoryId { get; set; } 
    [XmlElement("IsLive")] 
    public bool IsLive { get; set; } 
    [XmlElement("Match")] 
    public MatchModel[] Matches { get; set; } 
} 

public class MatchModel 
{ 
    [XmlElement("Name")] 
    public string Name { get; set; } 
    [XmlElement("ID")] 
    public int Id { get; set; } 
    [XmlElement("StartDate")] 
    public DateTime StartDate { get; set; } 
    [XmlElement("MatchType")] 
    public MatchType Matchtype { get; set; } 
    [XmlElement("Bet")] 
    public BetModel[] Bets { get; set; } 
} 

public enum MatchType 
{ 
    PreMatch, 
    Live 
} 

Antwort

0

In Visual Studio 2012 und später können Sie die XML-nehmen und bearbeiten gehen -> Einfügen Spezial -> Einfügen XML als Klassen Wenn Sie es nicht haben, gibt es Tools online, die das für Sie tun.

Gerade mit der Transformation zu helfen, hier ist das Ergebnis aus dem XML-Einfügen:

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class XmlSports 
{ 

    private XmlSportsSport[] sportField; 

    private System.DateTime createDateField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Sport")] 
    public XmlSportsSport[] Sport 
    { 
     get 
     { 
      return this.sportField; 
     } 
     set 
     { 
      this.sportField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public System.DateTime CreateDate 
    { 
     get 
     { 
      return this.createDateField; 
     } 
     set 
     { 
      this.createDateField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class XmlSportsSport 
{ 

    private XmlSportsSportEvent[] eventField; 

    private string nameField; 

    private ushort idField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Event")] 
    public XmlSportsSportEvent[] Event 
    { 
     get 
     { 
      return this.eventField; 
     } 
     set 
     { 
      this.eventField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public ushort ID 
    { 
     get 
     { 
      return this.idField; 
     } 
     set 
     { 
      this.idField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class XmlSportsSportEvent 
{ 

    private XmlSportsSportEventMatch[] matchField; 

    private string nameField; 

    private ushort idField; 

    private ushort categoryIDField; 

    private bool isLiveField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Match")] 
    public XmlSportsSportEventMatch[] Match 
    { 
     get 
     { 
      return this.matchField; 
     } 
     set 
     { 
      this.matchField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public ushort ID 
    { 
     get 
     { 
      return this.idField; 
     } 
     set 
     { 
      this.idField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public ushort CategoryID 
    { 
     get 
     { 
      return this.categoryIDField; 
     } 
     set 
     { 
      this.categoryIDField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public bool IsLive 
    { 
     get 
     { 
      return this.isLiveField; 
     } 
     set 
     { 
      this.isLiveField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class XmlSportsSportEventMatch 
{ 

    private XmlSportsSportEventMatchBet[] betField; 

    private string nameField; 

    private uint idField; 

    private System.DateTime startDateField; 

    private string matchTypeField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Bet")] 
    public XmlSportsSportEventMatchBet[] Bet 
    { 
     get 
     { 
      return this.betField; 
     } 
     set 
     { 
      this.betField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public uint ID 
    { 
     get 
     { 
      return this.idField; 
     } 
     set 
     { 
      this.idField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public System.DateTime StartDate 
    { 
     get 
     { 
      return this.startDateField; 
     } 
     set 
     { 
      this.startDateField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string MatchType 
    { 
     get 
     { 
      return this.matchTypeField; 
     } 
     set 
     { 
      this.matchTypeField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class XmlSportsSportEventMatchBet 
{ 

    private XmlSportsSportEventMatchBetOdd[] oddField; 

    private string nameField; 

    private uint idField; 

    private bool isLiveField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Odd")] 
    public XmlSportsSportEventMatchBetOdd[] Odd 
    { 
     get 
     { 
      return this.oddField; 
     } 
     set 
     { 
      this.oddField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public uint ID 
    { 
     get 
     { 
      return this.idField; 
     } 
     set 
     { 
      this.idField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public bool IsLive 
    { 
     get 
     { 
      return this.isLiveField; 
     } 
     set 
     { 
      this.isLiveField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class XmlSportsSportEventMatchBetOdd 
{ 

    private string nameField; 

    private uint idField; 

    private decimal valueField; 

    private decimal specialBetValueField; 

    private bool specialBetValueFieldSpecified; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public uint ID 
    { 
     get 
     { 
      return this.idField; 
     } 
     set 
     { 
      this.idField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public decimal Value 
    { 
     get 
     { 
      return this.valueField; 
     } 
     set 
     { 
      this.valueField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public decimal SpecialBetValue 
    { 
     get 
     { 
      return this.specialBetValueField; 
     } 
     set 
     { 
      this.specialBetValueField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlIgnoreAttribute()] 
    public bool SpecialBetValueSpecified 
    { 
     get 
     { 
      return this.specialBetValueFieldSpecified; 
     } 
     set 
     { 
      this.specialBetValueFieldSpecified = value; 
     } 
    } 
} 

Und der verwendete Code war einfach:

XDocument test = XDocument.Load("sportxml.xml"); 
var result = XMLHelper.FromXML<XmlSports>(test); 
+0

ich es versucht, aber scheint nicht zu funktionieren . https://msdn.microsoft.com/en-us/library/hh371548(v=vs.110).aspx – Prabulg

+0

Ich verwendete tatsächlich Ihre Klasse und Ihren Code und das Ergebnis des XML-Einfügens in Visual Studio 2015 und es funktionierte. Ich meine, ich kann nicht mal Code einfügen, weil ich es von deiner Frage kopiert habe. –

+0

Danke, dass ich in VS2015 eingefügt habe und meine Bugs jetzt funktioniert. – Prabulg

Verwandte Themen