OK; that helps a bit; now, I've tried that with:
using Google.Protobuf;
using SCL.PC;
using System;
using System.Xml.Serialization;
static class P
{
static void Main()
{
var obj = new ClientIdentifier
{
Context = ClientIdentifier.Types.Context.Cfid,
Value = ByteString.CopyFrom(00, 01, 02, 03)
};
var ser = new XmlSerializer(obj.GetType());
ser.Serialize(Console.Out, obj);
}
}
---
I **cannot** reproduce the error you're seeing, but I *do* get another error, as shown below.
This tells me, ultimately, that your model *isn't going to work with XmlSerializer*. And that's fine - the code is only *tested* against the specific API it is designed for - Google's protobuf library in this case.
If XmlSerializer on some down-level version of .NET has *even more* problems, that's... unexpected, but not amazingly surprising.
If you want to use multiple serializers, usually you should have a serialization DTO model *per serializer* - one that is designed for use with that serializer.
However, there may be another option *if you want*. Google's generated code that is contributing to the pain here. Looking at your generated code, it looks like your schema is something like:
syntax = "proto3";
message ClientIdentifier {
Context context = 1;
bytes value = 2;
}
enum Context {
Cfid = 0;
Csn = 1;
}
static class P
{
static void Main()
{
var obj = new ClientIdentifier
{
Context = Context.Cfid,
Value = new byte[] { 00, 01, 02, 03 }
};
var ser = new XmlSerializer(obj.GetType());
ser.Serialize(Console.Out, obj);
}
}
it now all works fine in my version of .NET, and I'm *guessing* that it'll work in yours too. This is using a **different library**, specifically protobuf-net rather than Google.ProtoBuf, but: the key point here is that they both implement the same specification so should be interchangeable.
Up to you!
---
XmlSerializer exception.
Unhandled exception. System.InvalidOperationException: There was an error reflecting type 'SCL.PC.ClientIdentifier'.
---> System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. Google.Protobuf.ByteString does not implement Add(System.Object).
at System.Xml.Serialization.TypeScope.GetEnumeratorElementType(Type type, TypeFlags& flags)
at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo)
at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)
at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at P.Main() in C:\Users\marc\source\repos\ConsoleApp14\Program.cs:line 16