3

I have been googling and prototyping with no success this idea and wanted to check that it is possible. I have WCF server client set up.

I have a object with datacontracts and datamembers. I am doing the desalinization on the client. The method is also on the client.

    [Serializable]
    [DataContract (Namespace = "www.doesnotmatter.com")]
    [XmlRoot("home")]
    public partial class BaseModel
    {
        [DataMember(IsRequired = false)]
        public string prop1
        { get; set; }

        [DataMember(IsRequired = false)]
        public string prop2
        { get; set; }

        [DataMember(IsRequired=false)]
        public string prop3
        { get; set; }

      }

I am trying to deserialize from xml to an object the method below is my function

public T FromXmlString<T>()
{
    var reader = new StringReader(xmlConfiguration);
    var serializer = new XmlSerializer(typeof(T), "www.doesnotmatter.com");
    var instance = (T)serializer.Deserialize(reader);
    reader.Dispose();
    return instance;
}

The xml is

   <home>
      <prop1>aaaaa</prop1>
      <prop2>bbbbb</prop2>
      <prop3>cccccc</prop3>
    </home>

I keep getting the error

InnerException = {"<Task xmlns=''> was not expected."}

So I am taking the XML above and want to deserialize into the BaseModel above. EDIT:

The base Model object is sitting on the server, I am on the client and have XML I want to deserialize into that given object (BaseModel). I think it is a namespace problem where it cannot deserialize from the xml into the object correctly but am not sure.

I am really unsure what the problem is, can anyone see it?

5
  • You need to provide more details of the class you wish to deserialize. It sounds like you're trying to deserialize to the base type instead of the derived class. P&B Commented Sep 7, 2012 at 15:09
  • Can you post the XML you're trying to deserialize? Commented Sep 7, 2012 at 15:14
  • the xml is above and I am deserializing into the class base model Commented Sep 7, 2012 at 15:34
  • Hard to understand why you're manually performing deserialization, that is what WCF is for. P&B Commented Sep 7, 2012 at 15:42
  • No - the object is sitting on the server, I am on the client and have XML i want to deserialize into that given object. I think it is a namespace problem where it cannot deserialize from the xml into the object correctly but am not sure Commented Sep 7, 2012 at 15:44

2 Answers 2

2

Change the 2nd parameter to your serializer to an empty string:

var serializer = new XmlSerializer(typeof(BaseModel), "");

This unit test passes:

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var xml = @"   <home>
      <prop1>aaaaa</prop1>
      <prop2>bbbbb</prop2>
      <prop3>cccccc</prop3>
    </home>";

            var reader = new StringReader(xml);
            var serializer = new XmlSerializer(typeof(BaseModel), "");
            var instance = (BaseModel)serializer.Deserialize(reader);

            Assert.AreEqual("aaaaa", instance.prop1);
            Assert.AreEqual("bbbbb", instance.prop2);
            Assert.AreEqual("cccccc", instance.prop3);
        }
    }

    [Serializable]
    [DataContract(Namespace = "www.doesnotmatter.com")]
    [XmlRoot("home")]
    public partial class BaseModel
    {
        [DataMember(IsRequired = false)]
        public string prop1
        { get; set; }

        [DataMember(IsRequired = false)]
        public string prop2
        { get; set; }

        [DataMember(IsRequired = false)]
        public string prop3
        { get; set; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It could be that the class already has an XML root that you're failing to correctly override. It's difficult to tell as there's not much information.

Try

var serializer = new XmlSerializer(typeof(T), new XmlRootAttribute("home"));

4 Comments

Thanks - I will try this, I can provide any other information you think would help.
If you're hoping to send the deserialized object to the server, it would be better design to deserialize to an intermediate class first (Class X), then construct your WCF object on the client side, and assign each parameter from the custom object you just deserialized (which is of class X). P&B.
Why is this a better design then going straight to the object I want to send. The xml has all the information for the object I want to fill this object and send it, why should I be using another object in the middle - seems over complicated and pointless as you say I will then to right code to add each prop which could be very very large..?
If you leave it as it is and the class on the server changes (say the properties get renamed), it won't break until runtime (the deserialization will fail). However, if you use an intermediary class and assign each property, a change on the server side will cause your code to not compile when each property is being assigned from class X, which means you can fix it before it runs. If the initial answer solved your problem please tick. P&B.

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.