views:

70

answers:

2

I have the following struct:

public struct Declarations
{
 public const string SchemaVersion = "urn:ean.ucc:gdsn:2";
}

And the SchemaVersion is used by some XmlElements which are trying to be serialized like this:

[XmlElement(Type=typeof(SomeGarbage),ElementName="moreJunk",IsNullable=false,Form=XmlSchemaForm.Unqualified,Namespace=Declarations.SchemaVersion)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public SomeType __someMember;

I'm getting the following error when I try to run sgen.exe on my dll:

Error 1 There was an error reflecting type 'BigHammer.CINParser.Messages.Declarations'
Error 2 The command ""C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" /force "C:\Users\mstoddard\teamserver_source\GDSN\CINParser\CINParser\bin\Debug\BigHammer.CINParser.dll" " exited with code 1.

I tried making the struct a class, no help
I tried adding a default constructor, no help

Thoughts?

===Update===
It looks like Visual Studio was hiding some information, I have The same classes defined in two different CLR namespaces but not XML namespaces:

Error: There was an error reflecting type 'BigHammer.CINParser.Messages.Declarations'. - Types 'BigHammer.CINParser.Messages.Declarations' and 'BigHammer.CINParser.Messages.urn_ean_ucc_2.Declarations' both use the XML type name, 'Declarations',from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type.

A: 

Xml serializer serializes only properties.

Wrap your variables.

class Cro
{
     public Cro
     {
          _atia="my const value";
     }
     private string _atia; // won't be serialized
     public string Atia //  will be serialized
     {
          get
          {
               return _atia;
          }
     }
}
Daniel Mošmondor
Hm, on second thoughts, do you use XMLSerializer at all?
Daniel Mošmondor
I don't use XmlSerializer, I use sgen.exe. I think it does the same thing, but at compile time. http://msdn.microsoft.com/en-us/library/bk3w6240(VS.80).aspx
MStodd
Can you try wrapping your members? When I use XML serializer, I always have private fields and public members that get serialized.
Daniel Mošmondor
Problem is that the only member in that struct is a const. Even if I change it to a static, I get "An attribute argument must be a constant expression, typeof expression..."
MStodd
Can you try to wrap it? Create only get method for it...
Daniel Mošmondor
The member being accessed needs to be const to ensure that it doesn't change at runtime. That would screw up serialization.
MStodd
No it doesn't. Initialize backing field in constructor, then provide ONLY get accessor. Or, if that's the problem for the serializer, provide SET accessor that doesn't touch your backing field.
Daniel Mošmondor
+1  A: 

Serialisation only applies to instance data.

Static/Const are data tied to the class.

Eg In the given app domain Declarations.SchemaVersion can only be a single value. So there is no instance data to serialise.

Thomas James