5

I have the following C# class

[XmlRoot("Customer")]
public class MyClass
{
    [XmlElement("CustId")]
    public int Id {get;set;}

    [XmlElement("CustName")]
    public string Name {get;set;}
}

I then use the following function serialise the class object to Xml

 public static XmlDocument SerializeObjectToXML(object obj, string sElementName)
 {
    XmlSerializer serializer = 
          new XmlSerializer(obj.GetType(), new XmlRootAttribute("Response"));

    using (MemoryStream ms = new MemoryStream())
    {
       XmlDocument xmlDoc = new XmlDocument();
       serializer.Serialize(ms, obj);
       ms.Position = 0;
       xmlDoc.Load(ms);
    }
}

My current output to XML is like;

<Response>
  <CustId></CustId>
  <CustName></CustName>
</Response>

But how can I get the response to look like;

<Response>
  <Customer>
     <CustId></CustId>
     <CustName></CustName>
  </Customer>
</Response>

3 Answers 3

10

Change the XmlElementAttribute on MyClass (it's not actually valid there according to http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute(v=vs.110).aspx) to an XmlTypeAttribute:

    [XmlType("Customer")]
    public class MyClass
    {
        [XmlElement("CustId")]
        public int Id { get; set; }

        [XmlElement("CustName")]
        public string Name { get; set; }
    }

The serialization method can now be (identical to that in the question but without the second parameter in the constructor of XmlSerializer):

    public static XmlDocument SerializeObjectToXML(object obj, string sElementName)
    {
        XmlSerializer serializer = new XmlSerializer(obj.GetType());
        XmlDocument xmlDoc = new XmlDocument();
        using (MemoryStream ms = new MemoryStream())
        {

            serializer.Serialize(ms, obj);
            ms.Position = 0;
            xmlDoc.Load(ms);
        }

        return xmlDoc;
    }
4

You can create a response object containing your customer, because that is what your desired xml shows as well.

[XmlRoot("Response")] 
public class ResponseClass
{
  [XmlElement("Customer")]
  public Myclass Customer {get;set;}
}
2
  • 1
    That should be [XmlRoot("Response")] if that was the case, but he's already handling that elsewhere. Though I do think this would give the proper output.
    – Tobberoth
    Nov 8, 2013 at 12:01
  • 2
    You can't use XmlElementAttribute with a class. Nov 8, 2013 at 12:02
2

You could define them like this:

public class MyClass
{
    [XmlElement("Customer")]
    public Customer cust { get; set; } 

}
public class Customer
{
    [XmlElement("CustId")]
    public int Id { get; set; }

    [XmlElement("CustName")]
    public string Name { get; set; }
}

By the way, the [XmlElement("Customer")] is not valid on your example...

2
  • And just noticed that there are 2 answers that were posted while I was writing this :) ...
    – Noctis
    Nov 8, 2013 at 12:10
  • I changed my question from [XmlElement("Customer")] to [XmlRoot("Customer")] which it should of been
    – neildt
    Nov 8, 2013 at 12:22

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.