我有两个类:基类名称 Component 和名为 DBComponent 的继承类
[Serializable]
public class Component
{
private string name = string.Empty;
private string description = string.Empty;
}
[Serializable]
public class DBComponent : Component
{
private List<string> spFiles = new List<string>();
// Storage Procedure Files
[XmlArrayItem("SPFile", typeof(string))]
[XmlArray("SPFiles")]
public List<string> SPFiles
{
get { return spFiles; }
set { spFiles = value; }
}
public DBComponent(string name, string description)
: base(name, description)
{ }
}
[Serializable]
public class ComponentsCollection
{
private static ComponentsCollection instance = null;
private List<Component> components = new List<Component>();
public List<Component> Components
{
get { return components; }
set
{
components = value;
}
}
public static ComponentsCollection GetInstance()
{
if (ccuInstance == null)
{
lock (lockObject)
{
if (instance == null)
PopulateComponents();
}
}
return instance;
}
private static void PopulateComponents()
{
instance = new CCUniverse();
XmlSerializer xs = new XmlSerializer(instance.GetType());
instance = xs.Deserialize(XmlReader.Create("Components.xml")) as ComponentsCollection;
}
}
}
我想从 Xml 文件中读取\写入。我知道我需要为 DBComponent 类实现序列化,否则它不会读取它。但我找不到任何简单的文章。对于这个简单的场景,我发现的所有文章都太复杂了。
Xml 文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<ComponentsCollection>
<Components>
<DBComponent Name="Tenant Historical Database" Description="Tenant Historical Database">
<SPFiles>
<SPFile>Setup\TenantHistoricalSP.sql</SPFile>
</SPFiles>
</DBComponent>
<Component Name="Agent" Description="Desktop Agent" />
</Components>
</ComponentsCollection>
有人可以给我一个简单的例子来说明如何阅读这种 xml 文件以及应该实现什么?
谢谢
里奥