Solved

C# - seriallization  - duplicate

Posted on 2013-06-16
4
418 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
Comment Utility
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
Comment Utility
I try to use  [XmlElement] instead of  [XmlArray("SetupConfig")]. IT works better for me. Thanks
0
 

Author Closing Comment

by:tommym121
Comment Utility
Thanks for your help.
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

10 Experts available now in Live!

Get 1:1 Help Now