2011-01-11 8 views
1

名前は同じだがサブノード構造はまったく異なる2つのノードを定義できる必要があります。私はこのXMLスキーマを設計しませんでしたが、当分の間はそのまま使用することを余儀なくされました。私はそれがXMLであるすべてのものをひどく乱用することを認識していますが、あなたはそれを持っています。もちろん名前は同じで、子ノードが異なる2つのノードをシリアル化する

<order> 
    <ItemType type="Clubs"> 
     <Club num="1"> 
      <ClubName>Some Name</ClubName> 
      <ClubChoice>Something Else</ClubChoice> 
     </Club> 
    </ItemType> 
    <ItemType type="Gift" val="MailGreeting"> 
     <GiftName>MailGreeting</GiftName> 
     <GiftDescription></GiftDescription> 
     <GiftQuanity>1</GiftQuanity> 
    </ItemType 
</order> 

それははるかに複雑よりますが、あなたはその要旨を取得する:私はそれが見えるように必要なもの

私はXmlSerializerを使用していますが、本当にXDocumentを使用しないようにしたいと思います。

+0

これをXmlSerializerで試しましたか?何がうまくいかなかったのですか? –

+0

デシリアライズする必要がありますか? –

+0

デシリアライズする必要はありません。 –

答えて

5

ご注文は、あなたがこのような要素に名前を付けるためにシリアライザを伝えることができ、リストのプロパティが含まれているとされていない場合:

[XmlRoot("order")] 
public class Order 
{ 
    private Whatever whateverInstance; 
    [XmlElement("ItemType")] 
    public Whatever WhateverInstance 
    { 
     get { return whateverInstance; } 
     set { whateverInstance = value; } 
    } 

    private Something somethingInstance; 
    [XmlElement("ItemType")] 
    public Something SomethingInstance 
    { 
     get { return somethingInstance; } 
     set { somethingInstance = value; } 
    } 
} 

を、それが物事のリストならあなたにも同じ要素名を持って得ることができるが、

[XmlRoot("order")] 
public class Order 
{ 
    private ItemType[] itemTypes; 
    [XmlElement("ItemType")] 
    public ItemType[] ItemTypes 
    { 
     get { return itemTypes; } 
     set { itemTypes = value; } 
    } 
} 

[XmlInclude(typeof(Clubs))] 
[XmlInclude(typeof(Gift))] 
public abstract class ItemType 
{ 
    private string type = "None"; 
    [XmlAttribute] 
    public string Type 
    { 
     get { return type; } 
     set { type = value; } 
    } 
} 

public class Clubs : ItemType 
{ 
    public Clubs() 
    { 
     Type = "Clubs"; 
    } 

    private Club[] clubsArray; 
    [XmlElement("Club")] 
    public Club[] ClubsArray 
    { 
     get { return clubsArray; } 
     set { clubsArray = value; } 
    } 

} 

public class Club 
{ 
    private int num = 0; 
    [XmlAttribute("num")] 
    public int Num 
    { 
     get { return num; } 
     set { num = value; } 
    } 

    private string clubName = ""; 
    public string ClubName 
    { 
     get { return clubName; } 
     set { clubName = value; } 
    } 

    private string clubChoice = ""; 
    public string ClubChoice 
    { 
     get { return clubChoice; } 
     set { clubChoice = value; } 
    } 
} 

public class Gift : ItemType 
{ 
    public Gift() 
    { 
     Type = "Gift"; 
    } 

    private string val = ""; 
    [XmlAttribute("val")] 
    public string Val 
    { 
     get { return val; } 
     set { val = value; } 
    } 

    private string giftName = ""; 
    public string GiftName 
    { 
     get { return giftName; } 
     set { giftName = value; } 
    } 

    private string giftDescription = ""; 
    public string GiftDescription 
    { 
     get { return giftDescription; } 
     set { giftDescription = value; } 
    } 

    private int giftQuanity = 0; 
    public int GiftQuanity 
    { 
     get { return giftQuanity; } 
     set { giftQuanity = value; } 
    } 
} 

テスト:

 List<ItemType> list = new List<ItemType>(); 
     list.Add(new Clubs() { ClubsArray = new Club[] { new Club() { Num = 0, ClubName = "Some Name", ClubChoice = "Something Else" } } }); 
     list.Add(new Gift() { Val = "MailGreeting", GiftName = "MailGreeting", GiftDescription = "GiftDescription", GiftQuanity = 1}); 
     Order order = new Order(); 
     rder.ItemTypes = list.ToArray(); 

     XmlSerializer serializer = new XmlSerializer(typeof(Order)); 
     StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Stuff.xml"); 
     serializer.Serialize(sw, order); 
     sw.Close(); 
あなたは冗長 xsi:Type属性を取得します

出力:

<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <ItemType xsi:type="Clubs" Type="Clubs"> 
    <Club num="0"> 
     <ClubName>Some Name</ClubName> 
     <ClubChoice>Something Else</ClubChoice> 
    </Club> 
    </ItemType> 
    <ItemType xsi:type="Gift" Type="Gift" val="MailGreeting"> 
    <GiftName>MailGreeting</GiftName> 
    <GiftDescription>GiftDescription</GiftDescription> 
    <GiftQuanity>1</GiftQuanity> 
    </ItemType> 
</order> 
+0

よろしくお願いします。何らかの理由で、同じ名前の要素を定義できないと思っていました。私がこのプロジェクトの途中でテストをすることができてから数日経ったので、私は現在の方法を廃止する必要があるかどうかを見たいと思っていました。 –

関連する問題