当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Serialize two nodes with the same name but different child nodes

问题描述:

I need to be able to define two nodes with the same name but completely different subnode structures. I didn't design this XML schema but for the time being I'm forced to use it as is. I realize it's a terrible abuse of everything that is XML but there you have it.

What I need it to look like:

<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>

Of course it's far more complicated than but you get the gist.

I'm using XmlSerializer and would really like to avoid using XDocument but if that's what I need to do then so be it.

网友答案:

If your order contains properties and not a list you can tell the serializer to name the elements like this:

[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; }
    }
}

If it's a list of things you could get to have a identical element name as well but you will get a redundant xsi:Type attribute:

[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; }
    }
}

Test:

        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();

Output:

<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>
分享给朋友:
您可能感兴趣的文章:
随机阅读: