我正在尝试对RSS 2.0 Feed进行反序列化,我想考虑一些iTunes扩展,但不必将它们直接烘焙到主类中。
使用C#中的XML反序列化器可能会出现以下内容吗?
public class RssChannel
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("link")]
public string Link { get; set; }
....
[XmlElement(Namespace = "itunes")]
public iTunesExtensions iTunes { get; set; }
}
public class iTunesExtensions
{
[XmlElement("category")]
public string[] Categories { get; set; }
}
我希望能解析类似的内容:
<channel>
<itunes:category text="Society & Culture"/>
<itunes:category text="Society & Culture"/>
<itunes:category text="Society & Culture"/>
</channel>
是否有可能在更模块化的地方做这样的事情?还是我被困在主要课堂上?
答案 0 :(得分:-1)
bizzehdee,
要将第一级设置为&#34;&#34;,您需要将其设置为班级的根和名称:
[XmlRoot("channel")]
public class channel
对于以下示例,我将其保留为RssChannel。
我假设您打算使用比iTunes更多的平台,因此iTunes仍然拥有自己的类。使用较少的代码执行此操作的方法是使用列表而不是数组:
public List<iTunes> iTunes;
类别将拥有自己的类,因此您可以在任何平台上使用类别。注意使用XmlAttribute。这将包括同一行中类别的名称:
public class iTunes
{
public List<Category> Categories { get; set; }
}
public class Category
{
[XmlAttribute("category")]
public string category { get; set; }
}
您可以使用静态类来帮助您序列化和反序列化数据。以下是静态类中的两个有用的方法:
// Save XML data.
public static void SaveData(ref RssChannel instance) {}
// Retrieve XML data.
public static RssChannel DeserializeData() {}
使用这些方法的最佳方法是首先使用DeserializeData()方法获取RssChannel的实例,如:
RssChannel foo = StaticClassName.DeserializeData();
对其进行更改,然后通过将其作为对SaveData()方法的引用进行保存来保存该实例,如:
SaveData(ref foo);
以下是一个完整的工作示例:
public static class XML
{
// Serializes the passed instance of RssChannel into XML file, and saves to runtime memory.
public static void SaveData(ref RssChannel instance)
{
// Objects:
StreamWriter sw = new StreamWriter("yourXmlFile.xml");
XmlSerializer serializer = new XmlSerializer(typeof(RssChannel));
// Save data.
serializer.Serialize(sw, instance);
sw.Close();
}
// Deserializes data from the XML file, and returns the instance.
public static RssChannel DeserializeData()
{
// Objects:
RssChannel channelData = new RssChannel();
XmlSerializer serializer = new XmlSerializer(typeof(RssChannel));
List<iTunes> iTunesList = new List<iTunes>();
if (File.Exists("yourXmlFile.xml"))
{
FileStream stream = new FileStream("yourXmlFile.xml", FileMode.Open);
// Deserialize data.
channelData = (RssChannel)serializer.Deserialize(stream);
stream.Close();
// Add data from deserialized iTunes list to list instance.
if (channelData.iTunesList != null)
iTunesList = channelData.iTunesList;
}
// Add data to RootData object lists.
channelData.iTunesList = iTunesList;
return channelData;
}
}
[XmlRoot("RssChannel")]
public class RssChannel
{
[XmlAttribute("Title")]
public string Title; // { get; set; }
[XmlAttribute("Link")]
public string Link; // { get; set; }
public List<iTunes> iTunesList; // { get; set; }
}
public class iTunes
{
public List<Category> Categories; // { get; set; }
}
public class Category
{
[XmlAttribute("category")]
public string category; // { get; set; }
}
您可以使用这样的类和静态方法:
private void AnyMethod()
{
// To get an instance of your RssChannel class with all the data:
RssChannel rssChannel = XML.DeserializeData();
// Do anything with the data. Example below:
iTunes newITunes = new iTunes();
List<Category> categoryList = new List<Category>();
Category newCategory1 = new Category(); // Create new categories.
newCategory1.category = "Allegro";
categoryList.Add(newCategory1);
Category newCategory2 = new Category();
newCategory2.category = "Prestissimo";
categoryList.Add(newCategory2);
newITunes.Categories = categoryList; // Add the categories to list.
rssChannel.iTunesList.Add(newITunes); // Add that list to iTunes list.
// Now, to save the data, pass a reference to the instance we just worked on:
XML.SaveData(ref rssChannel);
}
这将产生一个看起来像的文件:
<?xml version="1.0" encoding="utf-8"?>
<RssChannel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<iTunesList>
<iTunes>
<Categories>
<Category category="Allegro" />
<Category category="Prestissimo" />
</Categories>
</iTunes>
</iTunesList>
</RssChannel>