Solved

C# - seriallization  - duplicate

Posted on 2013-06-16
4
432 Views
Last Modified: 2013-06-17
I try to serialize a class,  I got a duplicate tag <Setup>
<Setup>
  <Setup entry="1" type="Long">

How can I get rid of it.  This is my code
    [XmlRootAttribute("Setups", IsNullable = false)]
    public class Setups
    {
        public int TradeType;
        // The XmlArrayAttribute changes the XML element name
        // from the default of "OrderedItems" to "Items".
        [XmlArray("Setup")]
        [XmlArrayItem]    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;
        [XmlArray("Profit")]
        [XmlArrayItem("ProfitItem")]    public string[] _ProfitItem;
        [XmlArray("Stop")]
        [XmlArrayItem("StopItem")]
        public string[] _StopItem;
        [XmlElement]                    public int _EntryTicks;
        [XmlElement]                    public int _StopTicks;
        [XmlElement]                    public int _ProfitTicks;
        [XmlElement]                    public int _size;
    }
This is the output

<?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>99</TradeType>
  <Setup>
    <Setup entry="1" type="Long">
      <Name>Fade0</Name>
      <Entry>Band</Entry>
      <Profit>
        <ProfitItem>Turn1</ProfitItem>
        <ProfitItem>Item1</ProfitItem>
      </Profit>
      <Stop>
        <StopItem>Fixed1</StopItem>
        <StopItem>Auto1</StopItem>
      </Stop>
      <_EntryTicks>0</_EntryTicks>
      <_StopTicks>32</_StopTicks>
      <_ProfitTicks>4</_ProfitTicks>
      <_size>1</_size>
    </Setup>
    <Setup entry="2" type="Long">
      <Name>+5</Name>
      <Entry>Band</Entry>
      <Stop>
        <StopItem>Fixed</StopItem>
        <StopItem>Auto</StopItem>
      </Stop>
      <_EntryTicks>-5</_EntryTicks>
      <_StopTicks>7</_StopTicks>
      <_ProfitTicks>4</_ProfitTicks>
      <_size>1</_size>
    </Setup>
  </Setup>
</Setups>
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
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39251187
you need a root element to wrap the Setup elements.
you can't have something like:
<Setups>
 <TradeType>
<Setup>
..
<Setup>
..
<Setup>

since you use Setups as the root element of the xml, i'd use SetupConfig as root element of Setup elements:
    [XmlRootAttribute("Setups", IsNullable = false)]
    public class Setups
    {
        public int TradeType;
        [XmlArray("SetupConfig")]
        public Setup[] Setup;

    }

    public class Setup
    {
        [XmlAttribute("entry")]
        public int _entry;
        [XmlAttribute("type")]
        public string _typeString;
        [XmlIgnore]
        public string _type;
        [XmlElement("Name")]
        public string _name;
        [XmlElement("Entry")]
        public string _entryName;
        [XmlArray("Profit")]
        [XmlArrayItem("ProfitItem")]
        public string[] _ProfitItem;
        [XmlArray("Stop")]
        [XmlArrayItem("StopItem")]
        public string[] _StopItem;
        [XmlElement]
        public int _EntryTicks;
        [XmlElement]
        public int _StopTicks;
        [XmlElement]
        public int _ProfitTicks;
        [XmlElement]
        public int _size;
    }

Open in new window

0
 

Author Comment

by:tommym121
ID: 39254590
I try to use  [XmlElement] instead of  [XmlArray("SetupConfig")]. IT works better for me. Thanks
0
 

Author Closing Comment

by:tommym121
ID: 39254592
Thanks for your help.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39255182
Why grade C?
I gave u a working solution and more important i explained why your code didnt which is not less important.
That should be grade B at least.
Cheers
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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