[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

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>
0
tommym121
Asked:
tommym121
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
use this code:
public class Stop
    {
         [XmlArray("Stop")]
         [XmlArrayItem("StopItem")]
        public string[] StopItem;
    }

Open in new window

0
 
tommym121Author Commented:
thanks.  I need to remove. the class and put StopItem directly into the parent class
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now