在C#中将Xml反序列化为Object

时间:2016-02-17 20:54:23

标签: c# xml serialization

我有一些我要序列化的XML。我的代码运行没有错误,但序列化不成功,对象是空的而不是填充数据。该值只是类型值:{OrangeCdToCollectorz.OrangeCd}。我怀疑我的班级定义。这是我正在序列化的课程:

  [Serializable, XmlRoot(ElementName = "Collection")]
  [XmlType("Collection")]
  public class OrangeCd
  {
    [XmlRoot(ElementName = "Artists")]
    public class Artists
    {
      [XmlAttribute(AttributeName = "Various")]
      public string Various { get; set; }
      [XmlElement(ElementName = "Artist")]
      public List<Artist> Artist { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Genres")]
    public class Genres
    {
      [XmlElement(ElementName = "Genre")]
      public List<string> Genre { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "ReleaseDate")]
    public class ReleaseDate
    {
      [XmlAttribute(AttributeName = "Encoded")]
      public string Encoded { get; set; }
      [XmlText]
      public string Text { get; set; }
    }

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

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

    [XmlRoot(ElementName = "Track")]
    public class Track
    {
      [XmlElement(ElementName = "Artist")]
      public string Artist { get; set; }
      [XmlElement(ElementName = "Title")]
      public string Title { get; set; }
      [XmlAttribute(AttributeName = "Number")]
      public string Number { get; set; }
      [XmlAttribute(AttributeName = "Frames")]
      public string Frames { get; set; }
      [XmlAttribute(AttributeName = "Time")]
      public string Time { get; set; }
      [XmlElement(ElementName = "Comment")]
      public string Comment { get; set; }
      [XmlElement(ElementName = "File")]
      public File File { get; set; }
      [XmlAttribute(AttributeName = "Bitrate")]
      public string Bitrate { get; set; }
    }

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

    [XmlRoot(ElementName = "Volume")]
    public class Volume
    {
      [XmlElement(ElementName = "Tracks")]
      public Tracks Tracks { get; set; }
      [XmlAttribute(AttributeName = "ID")]
      public string ID { get; set; }
      [XmlAttribute(AttributeName = "Frames")]
      public string Frames { get; set; }
      [XmlAttribute(AttributeName = "Time")]
      public string Time { get; set; }
    }

    [XmlRoot(ElementName = "Volumes")]
    public class Volumes
    {
      [XmlElement(ElementName = "Volume")]
      public Volume Volume { get; set; }
    }

    [XmlRoot(ElementName = "Album")]
    public class Album
    {
      [XmlElement(ElementName = "Artists")]
      public Artists Artists { get; set; }
      [XmlElement(ElementName = "Title")]
      public string Title { get; set; }
      [XmlElement(ElementName = "Year")]
      public string Year { get; set; }
      [XmlElement(ElementName = "Genres")]
      public Genres Genres { get; set; }
      [XmlElement(ElementName = "Category")]
      public string Category { get; set; }
      [XmlElement(ElementName = "Format")]
      public string Format { get; set; }
      [XmlElement(ElementName = "Comment")]
      public string Comment { get; set; }
      [XmlElement(ElementName = "RefNo")]
      public string RefNo { get; set; }
      [XmlElement(ElementName = "ReleaseDate")]
      public ReleaseDate ReleaseDate { get; set; }
      [XmlElement(ElementName = "Label")]
      public string Label { get; set; }
      [XmlElement(ElementName = "CatalogNo")]
      public string CatalogNo { get; set; }
      [XmlElement(ElementName = "Country")]
      public string Country { get; set; }
      [XmlElement(ElementName = "Location")]
      public string Location { get; set; }
      [XmlElement(ElementName = "Musicians")]
      public Musicians Musicians { get; set; }
      [XmlElement(ElementName = "Volumes")]
      public Volumes Volumes { get; set; }
      [XmlAttribute(AttributeName = "ID")]
      public string ID { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
      [XmlAttribute(AttributeName = "Created")]
      public string Created { get; set; }
      [XmlAttribute(AttributeName = "IsCompilation")]
      public string IsCompilation { get; set; }
      [XmlAttribute(AttributeName = "Icon")]
      public string Icon { get; set; }
      [XmlAttribute(AttributeName = "Frames")]
      public string Frames { get; set; }
      [XmlAttribute(AttributeName = "Time")]
      public string Time { get; set; }
      [XmlElement(ElementName = "Path")]
      public string Path { get; set; }
    }

    [XmlRoot(ElementName = "File")]
    public class File
    {
      [XmlAttribute(AttributeName = "Size")]
      public string Size { get; set; }
      [XmlText]
      public string Text { get; set; }
    }

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

    [XmlRoot(ElementName = "Albums")]
    public class Albums
    {
      [XmlElement(ElementName = "All")]
      public All All { get; set; }
    }

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

    [XmlRoot(ElementName = "Formats")]
    public class Formats
    {
      [XmlElement(ElementName = "Format")]
      public List<string> Format { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Categories")]
    public class Categories
    {
      [XmlElement(ElementName = "Category")]
      public List<string> Category { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Statuses")]
    public class Statuses
    {
      [XmlElement(ElementName = "Status")]
      public List<string> Status { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Locations")]
    public class Locations
    {
      [XmlElement(ElementName = "Location")]
      public List<string> Location { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Sources")]
    public class Sources
    {
      [XmlElement(ElementName = "Source")]
      public List<string> Source { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Generations")]
    public class Generations
    {
      [XmlElement(ElementName = "Generation")]
      public List<string> Generation { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Qualities")]
    public class Qualities
    {
      [XmlElement(ElementName = "Quality")]
      public List<string> Quality { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Conditions")]
    public class Conditions
    {
      [XmlElement(ElementName = "Condition")]
      public List<string> Condition { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Packagings")]
    public class Packagings
    {
      [XmlElement(ElementName = "Packaging")]
      public List<string> Packaging { get; set; }
      [XmlAttribute(AttributeName = "Modified")]
      public string Modified { get; set; }
    }

    [XmlRoot(ElementName = "Collection")]
    public class Collection
    {
      [XmlElement(ElementName = "Albums")]
      public Albums Albums { get; set; }
      [XmlElement(ElementName = "Artists")]
      public Artists Artists { get; set; }
      [XmlElement(ElementName = "Formats")]
      public Formats Formats { get; set; }
      [XmlElement(ElementName = "Categories")]
      public Categories Categories { get; set; }
      [XmlElement(ElementName = "Genres")]
      public Genres Genres { get; set; }
      [XmlElement(ElementName = "Statuses")]
      public Statuses Statuses { get; set; }
      [XmlElement(ElementName = "Locations")]
      public Locations Locations { get; set; }
      [XmlElement(ElementName = "Sources")]
      public Sources Sources { get; set; }
      [XmlElement(ElementName = "Generations")]
      public Generations Generations { get; set; }
      [XmlElement(ElementName = "Qualities")]
      public Qualities Qualities { get; set; }
      [XmlElement(ElementName = "Conditions")]
      public Conditions Conditions { get; set; }
      [XmlElement(ElementName = "Packagings")]
      public Packagings Packagings { get; set; }
      [XmlAttribute(AttributeName = "Version")]
      public string Version { get; set; }
    }
  }

这是我的代码尝试使用针对相同XML的几种众所周知的技术进行反序列化,作为字符串const和来自文件:

  XmlSerializer serializer = new XmlSerializer(typeof(OrangeCd));
  using (TextReader reader = new StringReader(testData))
  {
    OrangeCd orangeCd = (OrangeCd)serializer.Deserialize(reader);
  }

  // Show the dialog and get result.
  DialogResult result = openFileDialog1.ShowDialog();
  if (result == DialogResult.OK) // Test result.
  {
    string file = openFileDialog1.FileName;
    try
    {
      var ser = new XmlSerializer(typeof(OrangeCd));
      using (var reader = XmlReader.Create(file))
      {
        var wrapper = (OrangeCd)ser.Deserialize(reader);
        XmlDocument _Doc = new XmlDocument();
        _Doc.Load(file);

        XmlSerializer deserializer = new XmlSerializer(typeof(OrangeCd));
        using (TextReader textReader = new StringReader(_Doc.OuterXml))
        {
          OrangeCd XmlData = (OrangeCd)deserializer.Deserialize(textReader);
        }


        ser = new XmlSerializer(typeof(OrangeCd));
        wrapper = (OrangeCd)ser.Deserialize(new StringReader(_Doc.OuterXml));

        _Doc = new XmlDocument();
        _Doc.Load(file);
        ser = new XmlSerializer(typeof(OrangeCd));
        wrapper = (OrangeCd)ser.Deserialize(new XmlNodeReader(_Doc.DocumentElement));
      }
    }
    catch (IOException ioException)
    {
      Console.WriteLine(ioException.Message); // <-- For debugging use.
    }
  }

这是源XML

的示例
<?xml version="1.0" encoding="UTF-8"?>
<Collection Version="1.0">
<Albums>
    <All>
        <Album ID="CACDFB69814A4B2D" Modified="5192EB05" Created="5192EB06" IsCompilation="1" Icon="6" Frames="318975" Time="1:10:53">
            <Artists Various="1"/>
            <Title>Hava Narghile Vol. 1</Title>
            <Year>2001</Year>
            <Genres>
                <Genre>Folk, World, &amp; Country</Genre>
                <Genre>Rock</Genre>
            </Genres>
            <Category>rock</Category>
            <Format>CDR</Format>
            <Comment></Comment>
            <RefNo>V-00300</RefNo>
            <ReleaseDate Encoded="65000000">2001</ReleaseDate>
            <Label>Bacchus Archives</Label>
            <CatalogNo>BA1162</CatalogNo>
            <Country>US</Country>
            <Location>Avant-Garde: Violin</Location>
            <Musicians>
                <Credit>
                    <Name>Suzy Splab</Name>
                    <Role>Artwork</Role>
                </Credit>
                <Credit>
                    <Name>Gökhan Aya</Name>
                    <Role>Compilation Producer</Role>
                </Credit>
                <Credit>
                    <Name>Jay Dobis</Name>
                    <Role>Compilation Producer</Role>
                </Credit>
            </Musicians>
            <Volumes>
                <Volume ID="EFA30400" Frames="318975" Time="1:10:53">
                    <Tracks>
                        <Track Number="01" Frames="10125" Time="2:15">
                            <Artist>Silûetler</Artist>
                            <Title>Lorke Lorke</Title>
                        </Track>
                        <Track Number="02" Frames="12150" Time="2:42">
                            <Artist>Kaygisizlar</Artist>
                            <Title>Sasirdim (Short Circuit)</Title>
                        </Track>
                        <Track Number="03" Frames="11250" Time="2:30">
                            <Artist>Mavi Isiklar</Artist>
                            <Title>Iyi Düsün Tasin (For Your Information)            </Title>
                        </Track>
                        <Track Number="04" Frames="10800" Time="2:24">
                            <Artist>Haramiler</Artist>
                            <Title>Çamlica Yolunda</Title>
                        </Track>
                        <Track Number="05" Frames="18000" Time="4:00">
                            <Artist>Yabancilar</Artist>
                            <Title>Agit</Title>
                        </Track>
                        <Track Number="06" Frames="15375" Time="3:25">
                            <Artist>Apaslar</Artist>
                            <Title>Gilgamis</Title>
                        </Track>
                        <Track Number="07" Frames="11175" Time="2:29">
                            <Artist>Les Mogol</Artist>
                            <Title>Hard Work</Title>
                        </Track>
                        <Track Number="08" Frames="18300" Time="4:04">
                            <Artist>Erkin Koray</Artist>
                            <Title>Anma Arkadas</Title>
                        </Track>
                        <Track Number="09" Frames="12600" Time="2:48">
                            <Artist>Baris Manço &amp; Kaygisizlar</Artist>
                            <Title>Flower Of Love</Title>
                        </Track>
                        <Track Number="10" Frames="14850" Time="3:18">
                            <Artist>Erkut Taçkin &amp; Okan Dincer &amp; Kontrastlar</Artist>
                            <Title>Mühür Gözlüm</Title>
                        </Track>
                        <Track Number="11" Frames="22950" Time="5:06">
                            <Artist>Çagrisim</Artist>
                            <Title>Divane Gönlüm</Title>
                        </Track>
                        <Track Number="12" Frames="13875" Time="3:05">
                            <Artist>Baris Manço &amp; Kaygisizlar</Artist>
                            <Title>Trip (Fairground)</Title>
                        </Track>
                        <Track Number="13" Frames="16800" Time="3:44">
                            <Artist>Les Mogol</Artist>
                            <Title>Berkay Oyun Havasi</Title>
                        </Track>
                        <Track Number="14" Frames="13800" Time="3:04">
                            <Artist>Bunalim</Artist>
                            <Title>Basak Saçlim</Title>
                        </Track>
                        <Track Number="15" Frames="16500" Time="3:40">
                            <Artist>Üç Hürel</Artist>
                            <Title>...Ve Olüm</Title>
                        </Track>
                        <Track Number="16" Frames="17550" Time="3:54">
                            <Artist>Erkin Koray &amp; Ter</Artist>
                            <Title>Hor Görme Garibi</Title>
                        </Track>
                        <Track Number="17" Frames="15900" Time="3:32">
                            <Artist>Baris Manço</Artist>
                            <Title>Ben Bilirim</Title>
                        </Track>
                        <Track Number="18" Frames="12375" Time="2:45">
                            <Artist>Koray Oktay</Artist>
                            <Title>Vefasiz Dost</Title>
                        </Track>
                        <Track Number="19" Frames="13350" Time="2:58">
                            <Artist>Ersen</Artist>
                            <Title>Sor Kendine</Title>
                        </Track>
                        <Track Number="20" Frames="10425" Time="2:19">
                            <Artist>Melih Faruk Serdar Saygun</Artist>
                            <Title>Gurbet Acisi</Title>
                        </Track>
                        <Track Number="21" Frames="19800" Time="4:24">
                            <Artist>Erkin Koray</Artist>
                            <Title>Saskin</Title>
                        </Track>
                        <Track Number="22" Frames="11025" Time="2:27">
                            <Artist>Gökçen Kaynatan</Artist>
                            <Title>Pencerenin Perdesini</Title>
                        </Track>
                    </Tracks>
                </Volume>
            </Volumes>
        </Album>
        <Album ID="504B700C9EEAF33B" Modified="4B80B794" Created="4B80B794" Icon="1" Frames="128775" Time="28:37">
            <Artists>
                <Artist>The Minds</Artist>
            </Artists>
            <Title>Plastic Girls</Title>
            <Year>2003</Year>
            <Genres></Genres>
            <Category>rock</Category>
            <Format>CD</Format>
            <RefNo>T-0340</RefNo>
            <ReleaseDate Encoded="67000000">2003</ReleaseDate>
            <Location>Avant-Garde: Violin</Location>
            <Volumes>
                <Volume ID="75BB4B00" Frames="128775" Time="28:37">
                    <Tracks>
                        <Track Number="01" Frames="12610" Time="2:48">
                            <Title>Hot</Title>
                        </Track>
                        <Track Number="02" Frames="12087" Time="2:41">
                            <Title>Lost In A Crowd</Title>
                        </Track>
                        <Track Number="03" Frames="13833" Time="3:04">
                            <Title>Night Drive</Title>
                        </Track>
                        <Track Number="04" Frames="7997" Time="1:46">
                            <Title>Forbidden Friend</Title>
                        </Track>
                        <Track Number="05" Frames="10633" Time="2:21">
                            <Title>Don't Touch</Title>
                        </Track>
                        <Track Number="06" Frames="9605" Time="2:08">
                            <Title>Plastic Girls</Title>
                        </Track>
                        <Track Number="07" Frames="12040" Time="2:40">
                            <Title>Smash Smash Smash!</Title>
                        </Track>
                        <Track Number="08" Frames="9937" Time="2:12">
                            <Title>Nerves</Title>
                        </Track>
                        <Track Number="09" Frames="7120" Time="1:34">
                            <Title>Sensation</Title>
                        </Track>
                        <Track Number="10" Frames="10733" Time="2:23">
                            <Title>Open The Door</Title>
                        </Track>
                        <Track Number="11" Frames="12072" Time="2:40">
                            <Title>My Place (Adverts)</Title>
                        </Track>
                        <Track Number="12" Frames="10108" Time="2:14">
                            <Title>Sex Vamp</Title>
                        </Track>
                    </Tracks>
                </Volume>
            </Volumes>
        </Album>
    </All>
</Albums>
<Artists Modified="5185B3B0">
    <Artist>
        <Name>Béla Bartók</Name>
        <SortName>Bela Bartok</SortName>
    </Artist>
    <Artist>
        <Name>Béla Fleck &amp; The Flecktones</Name>
        <SortName>Bela Fleck &amp; The Flecktones</SortName>
    </Artist>
    <Artist>
        <Name>Ceoltórí</Name>
        <SortName>Ceoltori</SortName>
    </Artist>
    <Artist>
        <Name>Die Doofen</Name>
        <SortName>Doofen, Die</SortName>
    </Artist>
    <Artist>
        <Name>Les Amis Creole</Name>
        <SortName>Amis Creole, Les</SortName>
    </Artist>
    <Artist>
        <Name>Los Activos</Name>
        <SortName>Activos, Los</SortName>
    </Artist>
    <Artist>
        <Name>Los Dug Dug's</Name>
        <SortName>Dug Dug's, Los</SortName>
    </Artist>
    <Artist>
        <Name>Los Peyotes</Name>
        <SortName>Peyotes, Los</SortName>
    </Artist>
    <Artist>
        <Name>Méav</Name>
        <SortName>Meav</SortName>
    </Artist>
    <Artist>
        <Name>The Minds</Name>
        <SortName>Minds, The</SortName>
    </Artist>
</Artists>
<Formats Modified="4B4BF36D">
    <Format>CD</Format>
    <Format>CDR</Format>
    <Format>2CD</Format>
    <Format>CD+DVD</Format>
    <Format>3&quot; CD</Format>
    <Format>5&quot; CD</Format>
    <Format>CD box set</Format>
    <Format>DVD</Format>
    <Format>MP3</Format>
    <Format>FLAC</Format>
</Formats>
<Categories Modified="00000000">
    <Category>blues</Category>
    <Category>classical</Category>
    <Category>country</Category>
    <Category>data</Category>
    <Category>folk</Category>
    <Category>jazz</Category>
    <Category>misc</Category>
    <Category>newage</Category>
    <Category>reggae</Category>
    <Category>rock</Category>
    <Category>soundtrack</Category>
</Categories>
<Genres Modified="4B513A8A">
    <Genre>A Cappella</Genre>
    <Genre>Acid</Genre>
    <Genre>Acid Jazz</Genre>
    <Genre>Vocal</Genre>
    <Genre>Xmas</Genre>
</Genres>
<Statuses Modified="00000000">
    <Status>Borrowed</Status>
    <Status>Heard</Status>
    <Status>Loaned</Status>
    <Status>Lost</Status>
    <Status>Ordered</Status>
    <Status>Owned</Status>
    <Status>Sold</Status>
    <Status>Want</Status>
</Statuses>
<Locations Modified="556FB027">
    <Location>Jazz: Experimental</Location>
    <Location>Avant-Garde: Violin</Location>
    <Location>Bluegrass/Old Timey: Instrumental</Location>
    <Location>Zappa &amp; Related</Location>
</Locations>
<Sources Modified="00000000">
    <Source>AM Radio</Source>
    <Source>Audience</Source>
    <Source>Bootleg</Source>
    <Source>FM Radio</Source>
    <Source>Internet Radio</Source>
    <Source>Official</Source>
    <Source>Soundtrack</Source>
    <Source>TV</Source>
</Sources>
<Generations Modified="00000000">
    <Generation>1st Gen</Generation>
    <Generation>2nd Gen</Generation>
    <Generation>3rd Gen</Generation>
    <Generation>4th Gen</Generation>
    <Generation>&gt;4th Gen</Generation>
    <Generation>Unknown</Generation>
</Generations>
<Qualities Modified="00000000">
    <Quality>Excellent</Quality>
    <Quality>Good</Quality>
    <Quality>Listenable</Quality>
</Qualities>
<Conditions Modified="00000000">
    <Condition>Acceptable</Condition>
    <Condition>Good</Condition>
    <Condition>Excellent</Condition>
    <Condition>Mint</Condition>
    <Condition>Sealed</Condition>
</Conditions>
<Packagings Modified="00000000">
    <Packaging>CD case</Packaging>
    <Packaging>Slim CD case</Packaging>
    <Packaging>Super CD case</Packaging>
    <Packaging>Double CD case</Packaging>
    <Packaging>DVD case</Packaging>
    <Packaging>Digipak</Packaging>
    <Packaging>Sleeve</Packaging>
</Packagings>
</Collection>

3 个答案:

答案 0 :(得分:2)

你的OrangeCd类没有任何属性,因此它实际上是一个空类,尽管你已在其中声明了内部类。

答案 1 :(得分:2)

您可能想要做的是反序列化为OrangeCd.Collection,不进入OrangeCd

答案 2 :(得分:-1)

我需要反序列化为OrangeCd.Collection。不是OrangeCd,正如@Hadriel指出的那样。