Solved

C# - seriallization  - duplicate

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 43
XSLT Display Label Name 1 27
split XML field into many fields from MS Access 21 44
XSLT 1.0 - Finding Substrings from the End of a String 3 32
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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