FabioD3Vx
FabioD3Vx

Reputation: 1

XML serialization duplicate tags

This is my class structure:

[Serializable]
[XmlRoot("ClientRequestAPI3")]
public class RequestModelAPI3
{
    [XmlElement("source")]
    public string Source { get; set; }

    [XmlElement("destination")]
    public string Destination { get; set; }

    [XmlArray("packages")]
    public Packages[] Packages { get; set; }
}

[Serializable]
[XmlRoot("packages")]
public class Packages
{        
    [XmlElement("package")]
    public int Package { get; set; }
}

The XML it is generating is:

<?xml version="1.0" encoding="utf-16"?>
<ClientRequestAPI3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <source>Custom Source</source>
  <destination>Custom Destination</destination>
  <packages>
    <Packages>
      <package>1</package>
    </Packages>
    <Packages>
      <package>3</package>
    </Packages>
  </packages>
</ClientRequestAPI3>

But what I'm looking for is:

<?xml version="1.0" encoding="utf-16"?>
<ClientRequestAPI3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <source>Custom Source</source>
  <destination>Custom Destination</destination>
  <packages>
      <package>1</package>
      <package>3</package>
  </packages>
</ClientRequestAPI3>

So what is missing so I can have it serialized like above?

Thanks in advance, I much appreciate your time looking into it.

Upvotes: 0

Views: 166

Answers (2)

It all makes cents
It all makes cents

Reputation: 4983

Try the following:

using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

namespace XmlSerializationFD
{
    [XmlRoot("ClientRequestAPI3")]
    public class RequestModelAPI3
    {
        [XmlElement("source", Order = 1)]
        public string Source { get; set; }

        [XmlElement("destination", Order = 2)]
        public string Destination { get; set; }

        [XmlElement("packages", Order = 3)]
        public PackageInfo Packages = new PackageInfo();
       
    }


    public class PackageInfo
    {
        [XmlElement("package")]
        public List<int> Package { get; set; } = new List<int>();

        public void AddPackage(int id)
        {
            this.Package.Add(id);
        }
    }
}

Upvotes: 0

Alberto
Alberto

Reputation: 15941

Something like:

[Serializable]
[XmlRoot("ClientRequestAPI3")]
public class RequestModelAPI3
{
    [XmlElement("source")]
    public string Source { get; set; }

    [XmlElement("destination")]
    public string Destination { get; set; }

    [XmlArray("packages")]
    [XmlArrayItem("package")]
    public Packages[] Packages { get; set; }
}

[Serializable]
public class Packages
{  
    [XmlText]
    public int Package { get; set; }
}

But the [XmlText] and the additional class seems to me a bit smelly...

I think it would be better to declare the packages array as an int array:

[XmlArray("packages")]
[XmlArrayItem("package")]
public int[] Packages { get; set; }

Upvotes: 1

Related Questions