Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# - XML serialization

Posted on 2013-06-15
2
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

618 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