0

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>
2
  • how is your xml file structured now? Commented Oct 21, 2013 at 11:07
  • I will update the question now with my current results. Commented Oct 21, 2013 at 11:25

4 Answers 4

3

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; }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I am new here. some part of code block (class name declaration line)is rendered as simple text
2

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

1 Comment

I didn't get a chance to test this before I'd managed to fix my solution (as posted above) - this is a nice easy way to fix it in future so thanks anyway.
1

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>

Comments

0

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; }
}

1 Comment

This is really dangerous, it won't behave as a List<gsm> unless you overload all the methods. Ex recipients.Add(new gsm()) will add an element not enumerated by recipients.GetEnumerator().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.