WCF Service Reference - Getting “XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side

后端 未结 6 1623
醉梦人生
醉梦人生 2021-02-03 23:54

I have a smart client application communicating with its server via WCF. Data is created on the client and then sent over the service to be persisted. The server and client use

6条回答
  •  别那么骄傲
    2021-02-04 00:16

    Either use full properties with [Serializable], or use [DataContract] and [DataMember].

    The following was giving me an error, probably because .Net was creating a backing variable under the hood, with some character that the XmlSerializer didn't like.

    [Serializable]
    public class MyClass
    {
        public int MyValue { get; private set; }
        ...
    }
    

    Either create full properties

    [Serializable]
    public class MyClass
    {
        int _myValue;
        public int MyValue
        {
            get { return _myValue; }
            private set { _myValue = value; }
        }
        ...
    }
    

    Or use the DataContract and DataMember attributes

    [DataContract]
    public class MyClass
    {
        [DataMember]
        public int MyValue { get; private set; }
        ...
    }
    

提交回复
热议问题