Link to home
Start Free TrialLog in
Avatar of tommym121
tommym121Flag for Canada

asked on

C# - XML serialization

I like to create a class with proper XML tag and will write the output into
<?xml version="1.0" encoding="utf-8"?>
<Setups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TradeType>0</TradeType>
<Setup entry="1" type="Long">
      <Name>Fade0</Name>
      <Entry>Band</Entry>
      <Stop>
            <StopItem>Fixed</StopItem>
            <StopItem>Auto</StopItem>            
      </Stop>
      <EntryTicks>-0</EntryTicks>
      <StopTicks>32</StopTicks>
      <ProfitTicks>4</ProfitTicks>
      <Size>1</Size>
</Setup>

I have created the following class.

    [XmlRootAttribute("Setups", IsNullable = false)]
    public class Setups
    {
        public int TradeType;
        // The XmlArrayAttribute changes the XML element name
        // from the default of "OrderedItems" to "Items".
        [XmlArrayAttribute("Setup")]    public Setup[] Setup;

    }

    public class Setup
    {
        // The XmlAttribute instructs the XmlSerializer to serialize the
        // Name field as an XML attribute instead of an XML element (the
        // default behavior).
        [XmlAttribute("entry")] public int entry;
        [XmlAttribute("type")]  public string typeString;
        [XmlIgnore]             public string type;
        [XmlElement("Name")]    public string name;
        [XmlElement("Entry")]   public string entryName;
        public Stop stop;
        [XmlElement]            public int EntryTicks;
        [XmlElement]            public int StopTicks;
        [XmlElement]            public int ProfitTicks;
        [XmlElement]            public int size;
    }
    public class Stop
    {
        [XmlArrayAttribute("StopItem")]
        public string[] StopItem;
    }

All seems to work except StopItem.  It generate the following instead.  
<Stop>
        <StopItem>
                 <string>Fixed</string>
                 <string>Auto</string>
        </StopItem>
</Stop>

But I would like to have the following format in my xml file.  What do I need to modify my C# class to generate the following.
      <Stop>
            <StopItem>Fixed</StopItem>
            <StopItem>Auto</StopItem>            
      </Stop>
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tommym121

ASKER

thanks.  I need to remove. the class and put StopItem directly into the parent class