问题描述
我想能够将DateTime与不是服务器的特定时区序列化,也不是客户端时间。基本上,任何时区。
是否可以覆盖.Net2.0 webservices中的DateTime序列化?
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?
我使用xsd.exe编译xmlschema,所以我试图使用XmlSchemaImporter。
I compile an xmlschema using xsd.exe, so I made an attempt using XmlSchemaImporter.
Onserialize示例显示值更改,但不更改输出格式。
The OnSerialize examples show value changes, but not changes to the output format.
XmlSchemaImporter它进入gac,运行xsd.exe和生成的代码,有我想要的类...但该类是一个属性,最终不能被反映。
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.
生成的代码
[System.Xml.Serialization.XmlAttributeAttribute()]
public Cuahsi.XmlOverrides.W3CDateTime dateTime {
get {
return this.dateTimeField;
}
set {
this.dateTimeField = value;
}
}
XmlSchemaImporter
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; }
}
}
附录1:
我只是累了DateTimeoffset,当类被标记时仍然会导致错误:
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;
}
}
推荐答案
Don直接序列化 DateTimeOffset
,但序列化一个字符串:
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 */ }
}
这篇关于自定义DateTime XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!