Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6560419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:27:25+00:00 2026-05-25T13:27:25+00:00

I have 2 service reference (WCF). the first visual studio generate code using DataContractSerializer

  • 0

I have 2 service reference (WCF).

  • the first visual studio generate code using DataContractSerializer
  • the second one, visual studio generates code using XmlSerializer

I can’t change anything on the web server side.

So I’m creating an object aggregating objects from both references.

How can I serialize this object so it respect the serialization specification for a DataContractSerializer and for a XmlSerializer.
If I use a DataContractSerializer I’ll have every field from my reference 2 serialized like that

<dataField>

instead of

<data>

because it serializes only the private fields by default (??)

and if I use a XmlSerializer every string array in my reference 2 will be serialized like that

<myArray>
<string>test</string>
<string>test</string>
</myArray>

instead of

<myArray>
<url>test</url>
<url>test</url>
</myArray>

because it ignores the attribute CollectionDataContractAttribute which specified how to serialize every item in the array.

So what would be your solution ?

My first solution would be to add all of them as good ol’ web reference but maybe there is some nice solution.

EDIT :

here is the declaration of the type from the 1/ web service (datcontract,wcf style)

    [System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="productInformations", Namespace="http://abcedf.net/")]
[System.SerializableAttribute()]
public partial class productInformations : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:27:25+00:00Added an answer on May 25, 2026 at 1:27 pm

    The first problem: since the types generated by “Add Web Reference” are decorated with [Serializable] by default, the serializable model used by the DataContractSerializer is that all fields (public or not) are serialized. If you decorate the type with [DataContract] and the members you want serialized (the properties) with [DataMember]. A type can have the attributes for both serializers without problems, as shown below.

    public class StackOverflow_7348240
    {
        [Serializable]
        [DataContract(Name = "myRoot", Namespace = "")]
        [XmlRoot(ElementName = "myRoot", Namespace = "")]
        public class MyType
        {
            private string dataField;
    
            [XmlElement(ElementName = "data")]
            [DataMember(Name = "data")]
            public string Data
            {
                get { return this.dataField; }
                set { this.dataField = value; }
            }
        }
    
        public static void Test()
        {
            MyType obj = new MyType { Data = "hello world" };
    
            MemoryStream ms = new MemoryStream();
            new DataContractSerializer(obj.GetType()).WriteObject(ms, obj);
            Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
    
            ms.SetLength(0);
            new XmlSerializer(obj.GetType()).Serialize(ms, obj);
            Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
        }
    }
    

    For the second issue, if the collection type is a member of another object, you can also add the appropriate attributes (this time the ones for XML serialization), namely [XmlArray] (to specify the collection name) and [XmlArrayItem] (to specify the item name), and you can have the same type serialized the same way, like in the example below.

    public class StackOverflow_7348240_b
    {
        [DataContract(Name = "myRoot", Namespace = "")]
        [XmlRoot(ElementName = "myRoot", Namespace = "")]
        public class MyType
        {
            [DataMember(Name = "myArray")]
            [XmlArray(ElementName = "myArray")]
            [XmlArrayItem(ElementName = "url")]
            public MyArray myArray;
        }
    
        [CollectionDataContract(Name = "myArray", Namespace = "", ItemName = "url")]
        [XmlType(Namespace = "")]
        [XmlRoot(ElementName = "myArray", Namespace = "")]
        public class MyArray : List<string>
        {
        }
    
        public static void Test()
        {
            MyType obj = new MyType { myArray = new MyArray { "one", "two" } };
    
            MemoryStream ms = new MemoryStream();
            new DataContractSerializer(obj.GetType()).WriteObject(ms, obj);
            Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
    
            ms.SetLength(0);
            new XmlSerializer(obj.GetType()).Serialize(ms, obj);
            Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
        }
    }
    

    Update

    I understood that you wanted to merge the types “by hand”, but based on the comment this is not the case. In this case, if you want to use both Add Web Reference (AWR) and Add Service Reference (ASR), then you’ll need to fall back to the common serializer, which is the XmlSerializer. AWR always uses the XmlSerializer (XS), while ASR can use both that one and the DataContractSerializer (DCS). The DCS is the default one for ASR, but you can change it to use the other one. You have two options:

    • Use svcutil.exe (instead of ASR), and pass the /serializer:XmlSerializer command line
    • After adding the service reference, open the Reference.svcmap file (you may need to check the “Show All Files” option for the project), then change the option <Serializer> from Auto to XmlSerializer, then select “Update Service Reference”.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code where SprintServiceClient is a reference to a WCF Service-
I have a WCF service that I have to reference from a .net 2.0
I have created a reference to an IIS hosted WCF service in my ASP.NET
Adding a service reference to a web service (this is all WCF) in Visual
I have a silverlight library which I have added a wcf service reference. Call
I'm working on a WCF Service. I have one service operation Function getValues(Optional verbose
I have an ASP.net page that is creating a service reference to a WCF
I have my WCF service, I've created reference to it from MSTest project. Here
I am creating a WCF Service. It is my first one. I am receiving
Question: I Have a WCF test Service with a reference (.net 3.5) on Silverlight

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.