我想序列化一些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, & 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 & Kaygisizlar</Artist>
<Title>Flower Of Love</Title>
</Track>
<Track Number="10" Frames="14850" Time="3:18">
<Artist>Erkut Taçkin & Okan Dincer & 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 & 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 & 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 & The Flecktones</Name>
<SortName>Bela Fleck & 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" CD</Format>
<Format>5" 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 & 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>>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>
我需要反序列化成OrangeCd。收藏。不是OrangeCd,正如@Hadriel指出的那样。
您的OrangeCd类没有任何属性,因此它实际上是一个空类,尽管您在其中声明了内部类。
可能您想要做的是反序列化为OrangeCd。集合,没有进入橙色ECD
我在尝试“反序列化”对象时遇到以下错误: 我需要能够使用这个XStream库序列化/反序列化对象,甚至是那些没有参数构造函数的对象。
问题内容: 如何将json格式的数据字符串转换为 匿名对象 ? 问题答案: C#4.0添加了可以使用的动态对象。看看这个。
本文向大家介绍详解 C# 中XML对象的序列化和反序列化,包括了详解 C# 中XML对象的序列化和反序列化的使用技巧和注意事项,需要的朋友参考一下 这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候能够自动去加载这些数据,由于我们的这些Model中字段众多,如果单独进行保存那是
问题内容: 我需要将Objective- C对象序列化和反序列化为JSON以存储在CouchDB中。人们是否有通用解决方案最佳实践的示例代码?我看了几个JSON框架,它们在NSDictionary / NSArray级别停止了。即,许多框架会将NSDictionary / NSArray序列化和反序列化为JSON。但是我仍然要做将NSDictionary转换为Objective-C对象的工作。 为
我想反序列化这个xml标记: 转换成一个对象(某个java类)。 我有下一堂课: 我用Jackson XML这样做反序列化: 但是它给了我下一个错误: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: unrecognized field “” (class com.test.SomeXml), 未标记为可忽略(一个
问题内容: 我从看起来像这样的API获取JSON: 我尝试了几种方法来在c#对象中表示此JSON(太多内容无法在此处列出)。我已经尝试过使用列表和字典,这是我尝试表示它的最新示例: 这是我用来反序列化JSON的方法: 包含和。并且包含,但是是。因此,除了反序列化之外,什么都没有。 它应该很简单,但是由于某种原因我无法弄清楚正确的对象表示形式 问题答案: 要使用,即: 假设项目名称和随响应而变化,并