比泽迪,
为了将第一级设置为“”,您需要将其设置为类的根目录和名称:
[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>