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

17,486

Solution 1

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.

Solution 2

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

Solution 3

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!

Solution 4

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; }
    ...
}
Share:
17,486

Related videos on Youtube

alt_tab
Author by

alt_tab

Updated on June 18, 2022

Comments

  • alt_tab
    alt_tab almost 2 years

    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 the same domain classes via a shared dll and I'm using the handy "Add Service Reference" functionality in Visual Studio that wraps SvcUtil.exe and generates the client and proxy classes.

    I get the following error when trying to call the service:

    System.Xml.XmlException occurred
      Message=Name cannot begin with the '<' character, hexadecimal value 0x3C.
      Source=System.Xml
      LineNumber=0
      LinePosition=1
      StackTrace:
           at System.Xml.XmlConvert.VerifyNCName(String name, ExceptionType exceptionType)
      InnerException: 
    

    This is particularly troublesome because the service will work for weeks at a time without this error occurring and then suddenly and without warning it will appear again. I haven't been able to figure out what causes it at all. When it does happen, I will do a deep dive into researching how to fix it and usually don't come up with anything more than people who have experienced the same error while actually trying to serialize things to xml programmatically. I am using nothing but the generated client and proxies to try and send this data.

    I have looked into the generated proxies in the Service References\AwesomeService folder of my solution and see nothing that looks out of the ordinary. The only occurrences of angle brackets in the generated files are:

    • xml tags in the *.svcinfo, Reference.svcmap, AwesomeService.disco, AwesomeService.wsdl and *.xsd files
    • arguments to generic collections in the Reference.cs file

    The code I'm using to call the service is such:

    using (var client = new AwesomeServiceClient())
    {
        client.SaveAwesomeness(instanceOfAwesomeness);
    }
    

    This is the stack beggining with the first frame above the calling code listed above:

    System.Xml.dll!System.Xml.XmlConvert.VerifyNCName(string name, System.Xml.ExceptionType exceptionType) + 0xb5 bytes 
    System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.IsValidNCName(string name) + 0x27 bytes  
    System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.EncodeLocalName(string localName) + 0x1d bytes   
    System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.ImportDataMembers() + 0x2e1 bytes   
    System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.ClassDataContractCriticalHelper(System.Type type) + 0x10d bytes 
    System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(int id, System.RuntimeTypeHandle typeHandle, System.Type type) + 0x198 bytes   
    System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(int id, System.RuntimeTypeHandle typeHandle, System.Type type) + 0x57 bytes 
    System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(int id, System.RuntimeTypeHandle typeHandle) + 0x37 bytes  
    System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x49 bytes    
    [Lightweight Function]  
    System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) + 0x25 bytes 
    System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) + 0x18 bytes   
    System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) + 0x49 bytes  
    System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(System.Runtime.Serialization.XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) + 0xdf bytes  
    System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(System.Runtime.Serialization.XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) + 0x26 bytes 
    System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(System.Runtime.Serialization.XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) + 0x60 bytes    
    System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.WriteObject(System.Xml.XmlDictionaryWriter writer, object graph) + 0x2d bytes 
    System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo part, object graph) + 0x38 bytes 
    System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo part, object graph) + 0xbe bytes 
    System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameters(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo[] parts, object[] parameters) + 0x3e bytes  
    System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion version, string action, System.ServiceModel.Description.MessageDescription messageDescription, object returnValue, object[] parameters, bool isRequest) + 0x68 bytes   
    System.ServiceModel.dll!System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion version, object[] parameters, object returnValue, bool isRequest) + 0x7b bytes   
    System.ServiceModel.dll!System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer) + 0x4f bytes    
    System.ServiceModel.dll!System.ServiceModel.Channels.BodyWriter.WriteBodyContents(System.Xml.XmlDictionaryWriter writer) + 0xf8 bytes   
    System.ServiceModel.dll!System.ServiceModel.Channels.BodyWriterMessage.OnBodyToString(System.Xml.XmlDictionaryWriter writer) + 0x1f bytes   
    System.ServiceModel.dll!System.ServiceModel.Channels.Message.ToString(System.Xml.XmlDictionaryWriter writer) + 0xaa bytes   
    System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogTraceRecord.WriteTo(System.Xml.XmlWriter writer) + 0x166 bytes    
    System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogger.LogInternal(System.ServiceModel.Diagnostics.MessageLogTraceRecord record) + 0x77 bytes    
    System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogger.LogMessageImpl(ref System.ServiceModel.Channels.Message message, System.Xml.XmlReader reader, System.ServiceModel.Diagnostics.MessageLoggingSource source) + 0x104 bytes  
    System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogger.LogMessage(ref System.ServiceModel.Channels.Message message, System.Xml.XmlReader reader, System.ServiceModel.Diagnostics.MessageLoggingSource source) + 0x3a bytes   
    System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.PrepareCall(System.ServiceModel.Dispatcher.ProxyOperationRuntime operation, bool oneway, ref System.ServiceModel.Dispatcher.ProxyRpc rpc) + 0x436 bytes 
    System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.Call(string action, bool oneway, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation, object[] ins, object[] outs, System.TimeSpan timeout) + 0x12b bytes    
    System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(System.Runtime.Remoting.Messaging.IMethodCallMessage methodCall, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation) + 0x64 bytes    
    System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage message) + 0x6a bytes    
    mscorlib.dll!System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref System.Runtime.Remoting.Proxies.MessageData msgData, int type) + 0xee bytes
    

    What causes this and how do I prevent it? Or, also welcomed, how do I go about troubleshooting this further?

    • Edwin de Koning
      Edwin de Koning over 12 years
      I think it has something to do with the way .Net serializes the properties of your domain classes. Are you using the ISerializable interface, or DataContract attributes or perhaps some custom serialization?
    • alt_tab
      alt_tab over 12 years
      @edwin I am using the Serializable attribute, but after checking the whole domain graph, I'm embarrassed to say that there are some classes missing the attribute. I will try that. Thanks.
    • Gene
      Gene over 8 years
      I have received this a number of times and ended up here attempting to debug, only to later realize that it's a first chance exception that's caught internally and the object graph will successfully serialize/deserialize. Might not be relevant for your specific error, but just for folks (or myself) who come across this in the debugger in the future!
  • alt_tab
    alt_tab over 12 years
    I am not using DataTables, but rather am just shipping the domain graph across the service. I do have quite a few automatic properties, however, so I will see how the serializer does if I expand them.
  • alt_tab
    alt_tab over 12 years
    Replacing all automatic properties to be backed with instance variables solved this problem for me.
  • John Washburn
    John Washburn over 9 years
    This is not an answer because I don't have any data that is serialized by the WCF service I create. If I run the method
  • Eric Ouellet
    Eric Ouellet over 7 years
    @JohnWashburn, Sorry for the delay, I just read your comment now. I don't know WCF very well but I suspect that it serialize the data before sending it and deserialize the data on reception.
  • honzakuzel1989
    honzakuzel1989 over 6 years
    Add attributes [DataContract] and [DataMember] in class hierarchy works for me.
  • Grant Miller
    Grant Miller almost 6 years
    Most would find it useful to include a snippet of code to support your answer.
  • Maze90
    Maze90 over 4 years
    Just had this problem and I had a very long line, I couldn't see the end of it and it was missing the > so it could go either way.