Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

c# - Custom DateTime XML Serialization

I would like to be able to Serialize a DateTime with a specific Time Zone that is not the server, nor is it client time. Basically, any time zone. Is it possible to override the DateTime serialization, in .Net2.0 webservices?

I compile an xmlschema using xsd.exe, so I made an attempt using XmlSchemaImporter.

The OnSerialize examples show value changes, but not changes to the output format.

XmlSchemaImporter, loaded it into the gac, ran xsd.exe, and generated code that has the class I want... but that class is an attribute, which end up not being able to be reflected.

[InvalidOperationException: Cannot serialize member 'metadataDateTime' of type Cuahsi.XmlOverrides.W3CDateTime. XmlAttribute/XmlText cannot be used to encode complex types.]

Generated code

[System.Xml.Serialization.XmlAttributeAttribute()]
public Cuahsi.XmlOverrides.W3CDateTime dateTime {
    get {
        return this.dateTimeField;
    }
    set {
        this.dateTimeField = value;
    }
}

XmlSchemaImporter

public class ImportW3CTime : 
  System.Xml.Serialization.Advanced.SchemaImporterExtension
{
    public override string ImportSchemaType(string name, string ns,
        XmlSchemaObject context, XmlSchemas schemas,
        XmlSchemaImporter importer, CodeCompileUnit compileUnit, 
        CodeNamespace mainNamespace, CodeGenerationOptions options,
        CodeDomProvider codeProvider)
    {
        if (XmlSchema.Namespace == ns)
        {
            switch (name)
            {
                case "dateTime":
                    string codeTypeName = typeof(W3CDateTime).FullName;
                    CodeTypeDeclaration cls = 
                        new CodeTypeDeclaration("W3CDateTime");                     
                    cls.IsStruct = true;
                    cls.Attributes = MemberAttributes.Public;
                    cls.BaseTypes.Add("dateTime");
                    mainNamespace.Types.Add(cls);
                    return codeTypeName;
                default: return null;
            }
        }
        else { return null; }
    }
}

Addendum 1: I just tired DateTimeoffset, and that still causes an error when the class is tagged like:

[System.Xml.Serialization.XmlAttributeAttribute(DataType = "dateTime")]
public System.DateTimeOffset metadataDateTime {
    get {
        return this.metadataDateTimeField;
    }
    set {
        this.metadataDateTimeField = value;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't serialize the DateTimeOffset directly, but serialize a string instead:

// Don't serialize this one
[System.Xml.Serialization.XmlIgnore]
public System.DateTimeOffset metadataDateTime
{
    get { ... }
    set { ... }
}


// Serialize this one instead
[System.Xml.Serialization.XmlAttribute("metadataDateTime")]
public string metadataDateTimeXml
{
    get { /* format metadataDateTime to custom format */ }
    set { /* parse metadataDateTime from custom format */ }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...