简体   繁体   中英

Namespace in WCF DataContract with Xml deserialization

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?

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; }
    }
}

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM