2013-09-28 2 views
0

J'ai essayé d'utiliser xsd.exe pour créer une classe pour mon fichier XML et cela a semblé fonctionner, mais je peux énumérer la classe. J'ai même essayé d'utiliser Xsd2Code mais cela causerait cette erreur "Il y avait une erreur reflétant le type 'MySQlXML.XBMC'." quand j'ai essayé de énumérer dans la liste. S'il vous plaît laissez-moi savoir si vous avez des idées comment je pourrais obtenir le dénombrement de travailler.Création d'une classe à partir d'un fichier XML pouvant être énuméré via

Ceci est mon xml

<?xml version="1.0" encoding="utf-8"?> 
<XBMC> 
    <Show> 
    <IdShow>1</IdShow> 
    <TheTvDbId>248741</TheTvDbId> 
    <ShowName>2 Broke Girls</ShowName> 
    <Seasons> 
     <Season> 
     <Number>1</Number> 
     <Path>\\10.0.0.3\tv\2 Broke Girls\Season1\</Path> 
     </Season> 
     <Season> 
     <Number>3</Number> 
     <Path>\\10.0.0.3\tv\2 Broke Girls\Season 03\</Path> 
     </Season> 
    </Seasons> 
    </Show> 
    <Show> 
    <IdShow>164</IdShow> 
    <TheTvDbId>75682</TheTvDbId> 
    <ShowName>Bones</ShowName> 
    <Seasons> 
     <Season> 
     <Number>1</Number> 
     <Path>\\10.0.0.3\TV2\Bones\Season 01\</Path> 
     </Season> 
    </Seasons> 
    </Show> 
</XBMC> 

Voici comment sérialiser les données, mais lorsque je tente une foreach (XBMCShow show in test) je reçois foreach ne peut pas fonctionner sur des variables de type « MySQlXML.XBMC » parce que « MySQlXML.XBMC » fait ne contient pas une définition publique pour 'GetEnumerator'

TextReader reader = new StreamReader(_XMLFile); 
    XmlSerializer serializer = new XmlSerializer(typeof(XBMC)); 
    test = (XBMC)serializer.Deserialize(reader); 
    reader.Close(); 

Cette classe est générée XSD.exe.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml.Serialization; 


namespace MySQlXML 
{ 
    /// <remarks/> 
    public partial class XBMC 
    { 

     private XBMCShow[] showField; 

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

    /// <remarks/> 

    public partial class XBMCShow 
    { 

     public ushort idShowField; 

     public string theTvDbIdField; 

     public string showNameField; 

     private XBMCShowSeason[] seasonsField; 

     /// <remarks/> 
     public ushort IdShow 
     { 
      get 
      { 
       return this.idShowField; 
      } 
      set 
      { 
       this.idShowField = value; 
      } 
     } 

     /// <remarks/> 
     public string TheTvDbId 
     { 
      get 
      { 
       return this.theTvDbIdField; 
      } 
      set 
      { 
       this.theTvDbIdField = value; 
      } 
     } 

     /// <remarks/> 
     public string ShowName 
     { 
      get 
      { 
       return this.showNameField; 
      } 
      set 
      { 
       this.showNameField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlArrayItemAttribute("Season", IsNullable = false)] 
     public XBMCShowSeason[] Seasons 
     { 
      get 
      { 
       return this.seasonsField; 
      } 
      set 
      { 
       this.seasonsField = value; 
      } 
     } 
    } 

    /// <remarks/> 

    public partial class XBMCShowSeason 
    { 

     private byte numberField; 

     private string pathField; 

     /// <remarks/> 
     public byte Number 
     { 
      get 
      { 
       return this.numberField; 
      } 
      set 
      { 
       this.numberField = value; 
      } 
     } 

     /// <remarks/> 
     public string Path 
     { 
      get 
      { 
       return this.pathField; 
      } 
      set 
      { 
       this.pathField = value; 
      } 
     } 
    } 

} 

Répondre

0

essayer:

foreach (XBMCShow show in test.Show) { 
           ---- 
           ^

    if(show.ShowName == "Bones") { 
     ... 

vous avez manqué le point-Show partie

+0

Ok qui a fonctionné grâce, une dernière chose comment pourrais-je choisir le spectacle "Bones" du tableau par exemple – justinf

+0

voir si le fonctionne si :) – inquisitive

+0

Cool cela fonctionnerait si je voulais faire la même chose sans boucler tout le tableau normalement, vous pouvez utiliser l'index du tableau, mais ce n'est pas une option entendre vouloir quelque chose comme ça 'test.Show [] .ShowName == "Os" ' – justinf

Questions connexes