tommym121
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</ProfitTick s>
<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("StopIt em")]
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>
<?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</ProfitTick
<Size>1</Size>
</Setup>
I have created the following class.
[XmlRootAttribute("Setups"
public class Setups
{
public int TradeType;
// The XmlArrayAttribute changes the XML element name
// from the default of "OrderedItems" to "Items".
[XmlArrayAttribute("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("StopIt
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER