Jimbo
Jimbo

Reputation: 23004

XML Object Serialization

Im trying to post data to a web application that only accepts XML. I have created the objects in c# (as below) and am using the XmlSerializer to serialize the object to XML but cannot work out how to structure the objects in order to get the resulting XML that the receiving application requires:

The REQUIRED resulting XML

<recipients>
    <gsm messageId="clientmsgID1">number1</gsm>
    <gsm messageId="clientmsgID2">number2</gsm>
    <gsm messageId="clientmsgID3">number3</gsm>
    <gsm messageId="clientmsgID4">number4</gsm>
</recipients>

My objects

public class recipients
{
    public List<gsm> gsm{ get; set; }

    public recipients()
    {
        gsm = new List<gsm>();
    }
}

public class gsm
{
    [XmlText]
    public string number { get; set; }

    [XmlAttribute]
    public string messageId{ get; set; }
}

My resulting XML

<recipients>
    <gsm>
        <gsm messageId="clientmsgID1">number1</gsm>
    </gsm>
</recipients>

Upvotes: 0

Views: 264

Answers (4)

Jimbo
Jimbo

Reputation: 23004

SOLVED

I managed to resolve the issue myself before trying some of the other suggestions and thought I should post the fix here anyway.

I made the recipients implement List<gsm>. Done :)

public class recipients: List<gsm>
{
    private List<gsm> gsms{ get; set; }

    public recipients()
    {
        gsms = new List<gsm>();
    }

    public IEnumerator<gsm> GetEnumerator()
    {
        return gsms.GetEnumerator();
    }
}

public class gsm
{
    [XmlText]
    public string number { get; set; }

    [XmlAttribute]
    public string messageId { get; set; }
}

Upvotes: 0

Perevalov
Perevalov

Reputation: 348

You can mark your property gsm with an attribute [XmlElement("gsm")]

Full listing of classes:

public class recipients
{
    [XmlElement("gsm")]
    public List<gsm> gsm { get; set; }

    public recipients()
    {
        gsm = new List<gsm>();
    }
}

public class gsm
{
    [XmlText]
    public string number { get; set; }

    [XmlAttribute]
    public string messageId { get; set; }
}

Sample code:

var a = new recipients();
a.gsm.Add(new gsm() { messageId = "1", number = "aaa" });
a.gsm.Add(new gsm() { messageId = "2", number = "bbb" });
XmlSerializer serializer = new XmlSerializer(typeof(recipients));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(Console.Out, a, ns);

Output from my application:

<?xml version="1.0"?>
<recipients>
  <gsm messageId="1">aaa</gsm>
  <gsm messageId="2">bbb</gsm>
</recipients>

Upvotes: 1

Sarvesh
Sarvesh

Reputation: 279

use xsd.exe and try passing the xml file shown above. This will create a xsd file, use this xsd to create a cs class, and then use that cs class in your application which would create the same XMl on Serializing e.g

C:>xsd gsm.xml where gsm.xml will have the xml tags you have pasted above
and then
C:>xsd gsm.xsd /c to generate cs class

using System.Xml.Serialization;

public partial class recipients {
  private recipientsGsm[] itemsField;

  /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute("gsm", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
  public recipientsGsm[] Items {
    get { return this.itemsField; }
    set { this.itemsField = value; }
  }
}


public partial class recipientsGsm {
  private string messageIdField;
  private string valueField;

  /// <remarks/>
  [System.Xml.Serialization.XmlAttributeAttribute()]
  public string messageId {
    get { return this.messageIdField; }
    set { this.messageIdField = value; }
  }

  /// <remarks/>
  [System.Xml.Serialization.XmlTextAttribute()]
  public string Value {
    get { return this.valueField; }
    set { this.valueField = value; }
  }
}

Upvotes: 3

phil
phil

Reputation: 287

you only need to add

[System.Xml.Serialization.XmlElementAttribute("gsm", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]

at class recipients

public class recipients
{
    [System.Xml.Serialization.XmlElementAttribute("gsm", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public List<gsm> gsm{ get; set; }

    public recipients()
    {
        gsm = new List<gsm>();
    }
}

and it should work

Upvotes: 2

Related Questions