Sss
Sss

Reputation: 1529

Serialize class object giving wrong xml

In silverlight i have xml like:

<A>
    < A1>
       <B>
        < B1>a1.1</B1>
        <B1>a1.2</B1>
        </B>
    < /A1>

    < A1>
          <B>
          <B1>a1.3< /B1>
          <B1>a1.4</B1>
          </B>
    </A1>
</A>

My class are like this A.cs

namespace Check
{
  [XmlRoot(ElementName = "A")]
  public class A
  {

    [XmlElement("A1")]
       public List<A1> A1 { get; set; } 

  }
}

and A1.cs is:

namespace Check
{
  [XmlRoot(ElementName = "A1")]
  public class A1
  {  
        [XmlArray("B")]
        [XmlArrayItem("B1", typeof(string))]
        public List<string> B { get; set; }

  }
}

Class B.cs is

namespace Check
{
  [XmlRoot(ElementName = "B")]
  public class B
  {

        [XmlArray("B")]
        [XmlArrayItem("B1", typeof(string))]
        public List<string> B1 { get; set; }

  }
}

And i try to serialize ike this, In Xml.cs:

namespace Check
{
    public static class Xml
    {
        public static string ToXml(this object objectToSerialize)
        {
             var memory = new MemoryStream();
             var serial = new XmlSerializer(objectToSerialize.GetType());
             serial.Serialize(memory, objectToSerialize);  
            var utf8 = new UTF8Encoding();
            return utf8.GetString(memory.GetBuffer(), 0, (int)memory.Length);  
        }
    }
}

Ans Main Function class is :

namespace Check
{
    public  class ControlClass
    {
        public  void Main()
        {
            var a = new A() ;
            var xml = a.ToXml();

         }
    }
}

Is my approach to serialize correct ? If not please correct me ?

My output is:

<?xml version="1.0" encoding="utf-8" ?> 
  <A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /> 

Upvotes: 1

Views: 209

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063198

Yes, that is correct... ish. The reason it isn't showing more is because your A instance only has a single property (A1), and that has a null value. Add the data and it appears:

var a = new A {
    A1 = new List<A1> {
        new A1 { B = new List<string> { "a1.1", "a1.2" } },
        new A1 { B = new List<string> { "a1.3", "a1.4" } }
    }
};

Even an empty list would have helped. For that reason, you might prefer to use:

[XmlElement("A1")]
public List<A1> A1 { get { return a1; } }
private readonly List<A1> a1 = new List<Check.A1>();

and:

[XmlArray("B")]
[XmlArrayItem("B1", typeof(string))]
public List<string> B { get { return b1; } }
private readonly List<string> b1 = new List<string>();

along with:

var a = new A {
    A1 = {
        new A1 { B = { "a1.1", "a1.2" } },
        new A1 { B = { "a1.3", "a1.4" } }
    }
};

Note: to remove the default namespace alias declarations:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serial.Serialize(memory, objectToSerialize, ns);

This then has output:

<?xml version="1.0"?>
<A>
  <A1>
    <B>
      <B1>a1.1</B1>
      <B1>a1.2</B1>
    </B>
  </A1>
  <A1>
    <B>
      <B1>a1.3</B1>
      <B1>a1.4</B1>
    </B>
  </A1>
</A>

Upvotes: 1

Related Questions