Solved

C# - seriallization  - duplicate

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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