Solved

C# - XML serialization

Posted on 2013-06-15
2
384 Views
Last Modified: 2013-06-16
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
Comment
Question by:tommym121
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39250969
use this code:
public class Stop
    {
         [XmlArray("Stop")]
         [XmlArrayItem("StopItem")]
        public string[] StopItem;
    }

Open in new window

0
 

Author Closing Comment

by:tommym121
ID: 39251146
thanks.  I need to remove. the class and put StopItem directly into the parent class
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now