15

我有两个类:基类名称 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 文件以及应该实现什么?

谢谢
里奥

4

2 回答 2

21

不幸的是,您需要使用该属性告诉XmlSerializer您打算序列化或反序列化的类。XmlArrayItem()每种不同的类型也需要自己的元素名称。例如:

public class ComponentDerviedClass1: Component
public class ComponentDerivedClass2: Component
public class ComponentDerivedClass3: Component

// ...

public class ComponentsCollection
{
    [XmlArray("Components")]
    [XmlArrayItem("ComponentDerivedClass1", typeof(ComponentDerivedClass1))]
    [XmlArrayItem("ComponentDerivedClass2", typeof(ComponentDerivedClass2))]
    [XmlArrayItem("ComponentDerivedClass3", typeof(ComponentDerivedClass3))]
    public List<Component> Components
    {
        // ...
    }
}

这将读取一个 XML 文件,如:

<?xml version="1.0" encoding="utf-8" ?>
<ComponentsCollection>    
  <Components>
     <ComponentDerivedClass1>
         <!-- ... -->
     </ComponentDerivedClass1>
     <ComponentDerivedClass2>
         <!-- ... -->
     </ComponentDerivedClass2>
     <ComponentDerivedClass3>
         <!-- ... -->
     </ComponentDerivedClass3>
   </Components>  
</ComponentsCollection>

每个元素的多个实例可以存在(或不存在)。

于 2012-09-02T15:47:39.720 回答
13

不同场景的两种选择:告诉基类

[XmlInclude(typeof(DBComponent))]
public class Component
{
    private string name = string.Empty;
    private string description = string.Empty;  
}

或者:告诉集合:

[XmlArray]
[XmlArrayItem("Component", typeof(Component))]
[XmlArrayItem("DBComponent", typeof(DBComponent))]
public List<Component> Components {...}

实际上,如果您不想要外部节点(组件),也可以使用 [XmlElement(...)] 代替 [XmlArrayItem]。另外:您不需要[Serializable]。

于 2012-09-02T15:48:18.967 回答