Wie ändere ich das serialisierte Format von Zeitdaten? [Duplikat]C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Wie ändere ich das serialisierte Format von Zeitdaten? [Duplikat]

Post by Anonymous »

Ich sehe, dass die Frage geschlossen wurde, aber ich sehe keinen Zusammenhang zwischen der verlinkten Frage und meiner, es ist definitiv kein „Duplikat“...!

Ich habe eine C# .NET 4.5.1 Windows Forms-Anwendung, in der ich ein Objekt in eine XML-Datei serialisieren muss. Die Objektklasse wurde mit einem externen Tool erstellt und sollte nicht mehr geändert werden. Die Zeitdaten des Objekts werden auf folgende Weise serialisiert:

Code: Select all

10:35:59.0000000+01:00
und ich brauche es im Format HH:mm:ss, zum Beispiel:

Code: Select all

10:35:59
Da die Basisklasse nicht virtuell ist, kann ich sie nicht überschreiben, also habe ich versucht, sie mit einer neuen Klasse zu verbergen, erhalte jedoch eine InvalidOperationException-Ausnahme:

Code: Select all

System.InvalidOperationException
HResult=0x80131509
Message=There was an error reflecting type 'UblInvoice.InvoiceType'.
Source=System.Xml
StackTrace:
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 UblInvoice.Program.Main(String[] args) in C:\Temp\ConsoleApp2\ConsoleApp2\Program.cs:line 19

This exception was originally thrown at this call stack:
[External Code]

Inner Exception 1:
InvalidOperationException: There was an error reflecting property 'IssueTime'.

Inner Exception 2:
InvalidOperationException: There was an error reflecting type 'UblInvoice.IssueTimeType'.

Inner Exception 3:
InvalidOperationException: There was an error reflecting property 'Value'.

Inner Exception 4:
InvalidOperationException: Member IssueTimeType.Value of type System.String hides base class member TimeType.Value of type System.DateTime.  Use XmlElementAttribute or XmlAttributeAttribute to specify a new name.
Hier ist ein Auszug aus der XSD-generierten Klasse (sie ist groß, daher füge ich nur die relevanten Teile hinzu):

Code: Select all

/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
[XmlRootAttribute("IssueTime", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2", IsNullable=false)]
public partial class IssueTimeType : TimeType {
}

/// 
[XmlIncludeAttribute(typeof(IssueTimeType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(Namespace="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2")]
public partial class TimeType {

private System.DateTime valueField;

/// 
[XmlTextAttribute(DataType="time")]
public System.DateTime Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
Hier ist mein reproduzierbarer Code:

Code: Select all

internal class Program
{
static void Main(string[] args)
{
InvoiceType o = new InvoiceType();

o.IssueTime = new IssueTimeType()
{
Value = (new DateTime(2000, 1, 1, 15, 30, 0, 123)).ToString()
};

XmlSerializer s = new XmlSerializer(typeof(InvoiceType));
XmlTextWriter w = new XmlTextWriter(@"C:\temp\test.xml", Encoding.UTF8);
w.Formatting = Formatting.Indented;
s.Serialize(w, o);
w.Close();
}
}

public partial class IssueTimeType : TimeType
{
[XmlTextAttribute(DataType = "string")]
public new string Value
{
get
{
return base.Value.ToString("HH:mm:ss");
}
set
{
base.Value = DateTime.Parse(value);
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post