Solved

C# - XML serialization

Posted on 2013-06-15
2
386 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LINQ - C# to VB convertion 12 48
Not showing page correctly 3 30
Error when loading the database 16 57
Help with Syntax 9 27
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

15 Experts available now in Live!

Get 1:1 Help Now