Wiki Link: [discussion:2002]
Exception after converting Solution to .net 4.0 Beta1 

Jul 9 2009 at 12:01 PM
After conversioning the solution to targeting .Net 4.0 Beta1 and trying the 'TestHarness'-Example I'm getting an exception on 'BasicExpressionSerialization()'-Walkthrough:

System.Xml.XmlException: The '`' character, hexadecimal value 0x60, cannot be included in a name.

The exception takes place at: ExpressionSerializer.cs - Line 42

private XElement GenerateXmlFromExpressionCore(Expression e)
if (e == null)
return null;

XElement replace = ApplyCustomConverters(e);

if (replace != null)
return replace;

return new XElement(GetNameOfExpression(e), from prop in e.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance) select GenerateXmlFromProperty(prop.PropertyType, prop.Name, prop.GetValue(e, null)));

Are there any xml-changes in .Net4.0? Or what's wrong?

Bye Sefan

Sep 28 2009 at 11:27 AM
Hi, try replacing this two functions in source ExpressionSerializer.cs with this:

private Expression ParseExpressionFromXmlNonNull(XElement xml)
Expression expression = ApplyCustomDeserializers(xml);

if (expression != null)
return expression;
switch (xml.Name.LocalName)
case "BinaryExpression":
case "SimpleBinaryExpression":
case "LogicalBinaryExpression":
case "MethodBinaryExpression":
return ParseBinaryExpresssionFromXml(xml);
case "ConstantExpression":
case "TypedConstantExpression":
return ParseConstatExpressionFromXml(xml);
case "ParameterExpression":
case "PrimitiveParameterExpressionx00601":
case "TypedParameterExpression":
return ParseParameterExpressionFromXml(xml);
case "LambdaExpression":
return ParseLambdaExpressionFromXml(xml);
case "MethodCallExpression":
case "MethodCallExpressionN":
case "InstanceMethodCallExpressionN":
return ParseMethodCallExpressionFromXml(xml);
case "UnaryExpression":
return ParseUnaryExpressionFromXml(xml);
case "MemberExpression":
case "PropertyExpression":
case "FieldExpression":
return ParseMemberExpressionFromXml(xml);
case "NewExpression":
return ParseNewExpressionFromXml(xml);
case "ListInitExpression":
return ParseListInitExpressionFromXml(xml);
case "MemberInitExpression":
return ParseMemberInitExpressionFromXml(xml);
case "ConditionalExpression":
case "FullConditionalExpression":
return ParseConditionalExpressionFromXml(xml);
case "NewArrayExpression":
case "NewArrayInitExpression":
case "NewArrayBoundsExpression":
return ParseNewArrayExpressionFromXml(xml);
case "TypeBinaryExpression":
return ParseTypeBinaryExpressionFromXml(xml);
case "InvocationExpression":
return ParseInvocationExpressionFromXml(xml);
throw new NotSupportedException(xml.Name.LocalName);

private string GetNameOfExpression(Expression e)
if (e is LambdaExpression)
return "LambdaExpression";
return XmlConvert.EncodeName(e.GetType().Name);

... let me know.

Giuseppe Dimauro
Microsoft MSDN Regional Director, Italy

Sep 28 2009 at 11:28 AM
... or write me here: gdimauro at if you need whole tested project.


Feb 3 2011 at 9:22 AM
I tried those changes and throws another exception.
(Value cannot be null.
Parameter name: value) at line : private object GenerateXmlFromPrimitive(string propName, object value)

May I get working copy ?


Nov 26 2012 at 12:54 PM
Edited Nov 26 2012 at 1:06 PM

1) I've made changes to ExpressionSerializer.GenerateXmlFromPrimitive:
Instead of:
private object GenerateXmlFromPrimitive(string propName, object value)
return new XAttribute(propName, valueValue);

I added :

private object GenerateXmlFromPrimitive(string propName, object value)
object objValue = value == null ? Guid.NewGuid() : value;
return new XAttribute(propName, objValue);

I encountered the NullReference due to the expression's name which is null (and read only) so I guess that a Guid will serve well that purpose.

2) I've also added a case "PrimitiveParameterExpressionx00601": to ParseExpressionFromXmlNonNull since this is the way the serializer serializes the PrimitiveParameter type's name.

What do you say?

Page view tracker