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

久未见 提交于 2019-12-02 22:08:37

Have a look at your DataTables (if that's what you are using to transport data).

If the DataTable name is empty, then the Serializer might get confused and serialise things incorrectly.

Otherwise, if you are using typed, [Serializable] objects, I have found that sometimes the Serializer also gets confused if you use dynamic property declarations, eg:

public string MyName { get; set; }

But this would be a easily repeatable error.

Eric Ouellet

Personnally, I got the same problem with serialization of class hierarchy (not DataTables).

My problem was not related to automatic property at all, in fact I have many. My problem was that I forgot to include reference to "System.Runtime.Serialization" in one of my dll and I also forgot to add some attributes [DataContract] on some classes referenced by upper [DataMember] attributes up in the hierarchy.

To track my problem I started from my root class and removed some [DataMember] down the hierarchy until it pointed out the exact problem. It could takes some times depending on your hierarchy levels...

Hope it helps! Eric

A similar error tripped me up, but it turned out my config file (actually a clientconfig file for silverlight) contained the following

   <<security mode="Transport">
       <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
       <message clientCredentialType="Certificate" algorithmSuite="Default" />
   </security>

So sometimes messages about extra < characters should be taken literally!

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

In my case, one of the classes had a property, whose datatype was object. Something like this:

public class BuyAddOnServiceRequest
{
    object site_id
}

after changing this to:

public class BuyAddOnServiceRequest
{
    string site_id
}

it worked!

Ok just came across another scenario on this one. I had a Serializable type used as a parameter for one of my Operation Contract methods.

Commenting out this specific method from use allowed me to find the issue. In this case, the parameter was a model deserialized from a file, so I just replaced the implementation with a parameter of byte[] and ran deserialization logic at the other end.

While not necessarily an answer for all, in the case of parameter types on your Operation Contract methods that are Serializable, you may run into this exception also. I would imagine decorating them with correct DataContract attributes would assist in rectifying this problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!