15

私は2つのクラスを持っています:基本クラス名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

さまざまなシナリオの 2 つのオプション: 基本クラスに伝える

[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 {...}

実際、外側のノード (コンポーネント) が必要ない場合は、[XmlArrayItem] の代わりに [XmlElement(...)] を使用することもできます。また、[Serializable] は必要ありません。

于 2012-09-02T15:48:18.967 に答える