Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rid of "ArrayOf" prefix when serializing a collection in C#

Tags:

c#

I have a WebMethod which return a collection of PackageItemField:

[WebMethod]        
public List<PackageItemField> GetAvailablePackges()
{
    ... ;
}

What i get :

<ArrayOfPackageItemField xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <PackageItemField name="a">1</PackageItemField>
  <PackageItemField name="b">2</PackageItemField>
</ArrayOfPackageItemField>

What i need : Personalize the root element name

<SomeThingElse>
  <PackageItemField name="a">1</PackageItemField>
  <PackageItemField name="b">2</PackageItemField>
</SomeThingElse>

Solution which is not suitable for me : encapsulate the collecction as property inside a new class and use the follwing attributs

public class ClassTest
{
    private List<PackageItemField> coll;

    [XmlArray("SomeThingElse")]
    [XmlArrayItem("PackageItemField")]
    public List<PackageItemField> Coll
    {
        get
        {
            return coll;
        }
        set
        {
            coll = value;
        }
    }
}

Why not : because i'll have a root node as ClassTest int output.

Thanks for the help

like image 669
user852194 Avatar asked Dec 13 '25 07:12

user852194


1 Answers

Is this more what you had in mind?

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

public class TestService : System.Web.Services.WebService
{
    [WebMethod]
    [return: XmlArray("MyArray")]
    [return: XmlArrayItem("MyItem")]
    public List<string> HelloWorld()
    {
        return new List<string>() { "Hello World" };
    }
}

SOAP 1.2 response:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <MyArray>
        <MyItem>string</MyItem>
        <MyItem>string</MyItem>
      </MyArray>
    </HelloWorldResponse>
  </soap12:Body>
</soap12:Envelope>
like image 151
asawyer Avatar answered Dec 16 '25 07:12

asawyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!