Solved

C# - seriallization  - duplicate

Posted on 2013-06-16
4
429 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 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