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

  • Home
  • SEARCH
  • 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 205697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:34:46+00:00 2026-05-11T17:34:46+00:00

I have a WCF service which accepts a string as a paramter for one

  • 0

I have a WCF service which accepts a string as a paramter for one of its operation contracts. This string however has xml content in it.

I need to convert this into a class that is marked as a DataContract but is not exposed to the outside world.

I need to use the DataContractSerializer because the class members have the [DataMember] attribute set to a different name. Eg: the property Phone has the DataMember Name set as "Telephone", so when i deserialize the xmldocument using the normal serializer, i get an error as the deserializer looks for the Phone element which does not exist.

How do i de-serialize an XmlDocument using the DataContractSerializer? One constraint though is i cannot save the xmldocument to a file.

EDIT: Found an excellent article on serialization and de-serialization using DataContractSerializer here.

My client code:

string xmldata = "<Customer> + 
                System.Environment.NewLine+ 
                "<Age>1</Age>"+
                System.Environment.NewLine+
                "<BirthDate>1900-01-01T01:01:01.0000000-05:00</BirthDate>" + 
                System.Environment.NewLine+
                "<FistName>John</FistName>"+
                System.Environment.NewLine +
                "<LastName>Doe</LastName>" +
                System.Environment.NewLine +
                "</Customer>";

doc.LoadXml(xmldata); 
Service1Client a = new Service1Client();
a.GetData(doc.OuterXml.ToString());

My service code:

public string GetData(string per)
{
    string xmldata = per;
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmldata);
    XmlDemo.Person a = Person.Create();

    DataContractSerializer ser = new DataContractSerializer(a.GetType());
    StringWriter stringWriter = new StringWriter();
    XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
    xmlDoc.WriteTo(xmlWriter);

    MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(stringWriter.ToString()));
    stream.Position = 0;
    XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
    Person myContact = (Person)ser.ReadObject(reader, true);

    return string.Empty; 

}

My DataContract:

[Serializable]
[DataContract(Name = "Customer")]
public class Person
{
    private Person() {}
    [DataMember(Name = "FistName")]
    public string FName { get; set; }
    [DataMember(Name = "LastName")]
    public string LName { get; set; }
    [DataMember(Name = "Age")]
    public int Age { get; set; }
    [DataMember(Name = "BirthDate")]
    public DateTime DOB { get; set; }

    public static Person Create()
    {
        return new Person();
    }
}

I get this error at Person myContact = (Person)ser.ReadObject(reader, true);

Error in line 1 position 11. Expecting element 'Customer' from namespace 'http://schemas.datacontract.org/2004/07/XmlDemo'.. Encountered 'Element'  with name 'Customer', namespace ''.
  • 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-11T17:34:46+00:00Added an answer on May 11, 2026 at 5:34 pm

    Deserialize from string straight way

    MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes("<myXml />"));
    stream.Position = 0;
    XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
    MyContact myContact = (MyContact)ser.ReadObject(reader, true);
    

    Deserialize from XmlDocument

    StringWriter stringWriter = new StringWriter();
    XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
    xmlDoc.WriteTo(xmlWriter);
    
    MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(stringWriter.ToString()));
    stream.Position = 0;
    XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
    MyContact myContact = (MyContact)ser.ReadObject(reader, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

I'm still learning about REST and, in my test, came up with this scenario
I have a WCF Service hosted in IIS/ASP.NET that accepts HTTP Post ( not
Is it possible to have multiple listeners to messages carried by MSMQ? WCF appears
I am designing a WCF service which have some operations that i expose to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.