@Petrus 【KIM】 thanks for your support. The generated .CS file is different then original. I have to find out the fields thats generated in the class file.
I hope I can find that.
Rinaldo
cs0119 c# is a type which is not valid in the given context
Hi people,
i've got a question about serializing XML document (UBL)
code:
Invoice invoice(string file)
{
Invoice i = new Invoice();
string path = file;
XmlSerializer serializer = new XmlSerializer(typeof(Invoice));
//StreamReader reader = new StreamReader(path);
FileStream fs = new FileStream(file, FileMode.Open);
i = (Invoice)serializer.Deserialize(fs); // error CS0119 on invoice
// Error in XML document
fs.Close();
return i;
}
XML line gives error
<cac:TaxScheme>
What is what I doing wrong, in some cases it works but not with all
Greeting Rinaldo
Developer technologies C#
-
Rinaldo 396 Reputation points
2021-11-24T08:53:01.437+00:00
7 additional answers
Sort by: Most helpful
-
Petrus 【KIM】 546 Reputation points
2021-11-24T00:38:38.733+00:00 I test your code but no problem.
Please show me your xml file.Also, You can validate your xml with xsd.
https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.validate?view=net-6.0static void Main(string[] args) { invoice("D:\\Projects\\ConsoleApp1\\MyXml.xml"); } static MyXsd invoice(string file) { MyXsd i = new MyXsd(); string path = file; XmlSerializer serializer = new XmlSerializer(typeof(MyXsd)); //StreamReader reader = new StreamReader(path); FileStream fs = new FileStream(file, FileMode.Open); i = (MyXsd)serializer.Deserialize(fs); // error CS0119 on invoice // Error in XML document fs.Close(); return i; }
- XML File(MyXml.xml) : <MyXsd DATE="2021-12-31" />
- Command: XSD.EXE MyXml.xml
- Modify XSD: type="xs:dateTime"
- Command: XSD.EXE MyXml.xsd /c
-
Petrus 【KIM】 546 Reputation points
2021-11-24T07:46:24.81+00:00 Perform this way....
- XSD Invoice.xml
- XSD Invoice_app2.xsd Invoice_app1.xsd Invoice.xsd /c
C:\>XSD Invoice.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.8.3928.0]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file '...\Invoice.xsd'.C:\>XSD Invoice_app2.xsd Invoice_app1.xsd Invoice.xsd /c
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.8.3928.0]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file '...\Invoice_app2_Invoice_app1_Invoice.cs' -
Rinaldo 396 Reputation points
2021-11-23T08:12:58.437+00:00 Hi Jack, of course I can:
CLASS for UBL- List item using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization; namespace XMLublreader
{// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0. /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2", IsNullable = false)] public partial class Invoice { private decimal uBLVersionIDField; private string customizationIDField; private string profileIDField; private ID idField; private System.DateTime issueDateField; private string noteField; private System.DateTime taxPointDateField; private object accountingCostCodeField; private AccountingSupplierParty accountingSupplierPartyField; private AccountingCustomerParty accountingCustomerPartyField; private PaymentMeans paymentMeansField; private TaxTotal taxTotalField; private LegalMonetaryTotal legalMonetaryTotalField; private InvoiceLine[] invoiceLineField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public decimal UBLVersionID { get { return this.uBLVersionIDField; } set { this.uBLVersionIDField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string CustomizationID { get { return this.customizationIDField; } set { this.customizationIDField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string ProfileID { get { return this.profileIDField; } set { this.profileIDField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public ID ID { get { return this.idField; } set { this.idField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2", DataType = "date")] public System.DateTime IssueDate { get { return this.issueDateField; } set { this.issueDateField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string Note { get { return this.noteField; } set { this.noteField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2", DataType = "date")] public System.DateTime TaxPointDate { get { return this.taxPointDateField; } set { this.taxPointDateField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public object AccountingCostCode { get { return this.accountingCostCodeField; } set { this.accountingCostCodeField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public AccountingSupplierParty AccountingSupplierParty { get { return this.accountingSupplierPartyField; } set { this.accountingSupplierPartyField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public AccountingCustomerParty AccountingCustomerParty { get { return this.accountingCustomerPartyField; } set { this.accountingCustomerPartyField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public PaymentMeans PaymentMeans { get { return this.paymentMeansField; } set { this.paymentMeansField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public TaxTotal TaxTotal { get { return this.taxTotalField; } set { this.taxTotalField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public LegalMonetaryTotal LegalMonetaryTotal { get { return this.legalMonetaryTotalField; } set { this.legalMonetaryTotalField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("InvoiceLine", Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public InvoiceLine[] InvoiceLine { get { return this.invoiceLineField; } set { this.invoiceLineField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2", IsNullable = false)] public partial class ID { private byte schemeAgencyIDField; private bool schemeAgencyIDFieldSpecified; private string schemeIDField; private string valueField; /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public byte schemeAgencyID { get { return this.schemeAgencyIDField; } set { this.schemeAgencyIDField = value; } } /// <remarks/> [System.Xml.Serialization.XmlIgnoreAttribute()] public bool schemeAgencyIDSpecified { get { return this.schemeAgencyIDFieldSpecified; } set { this.schemeAgencyIDFieldSpecified = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public string schemeID { get { return this.schemeIDField; } set { this.schemeIDField = value; } } /// <remarks/> [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2", IsNullable = false)] public partial class AccountingSupplierParty { private AccountingSupplierPartyParty partyField; /// <remarks/> public AccountingSupplierPartyParty Party { get { return this.partyField; } set { this.partyField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyParty { private AccountingSupplierPartyPartyPartyName partyNameField; private AccountingSupplierPartyPartyPostalAddress postalAddressField; private AccountingSupplierPartyPartyPartyTaxScheme partyTaxSchemeField; private AccountingSupplierPartyPartyPartyLegalEntity partyLegalEntityField; private AccountingSupplierPartyPartyContact contactField; /// <remarks/> public AccountingSupplierPartyPartyPartyName PartyName { get { return this.partyNameField; } set { this.partyNameField = value; } } /// <remarks/> public AccountingSupplierPartyPartyPostalAddress PostalAddress { get { return this.postalAddressField; } set { this.postalAddressField = value; } } /// <remarks/> public AccountingSupplierPartyPartyPartyTaxScheme PartyTaxScheme { get { return this.partyTaxSchemeField; } set { this.partyTaxSchemeField = value; } } /// <remarks/> public AccountingSupplierPartyPartyPartyLegalEntity PartyLegalEntity { get { return this.partyLegalEntityField; } set { this.partyLegalEntityField = value; } } /// <remarks/> public AccountingSupplierPartyPartyContact Contact { get { return this.contactField; } set { this.contactField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyPartyPartyName { private string nameField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string Name { get { return nameField; } set { this.nameField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyPartyPostalAddress { private string streetNameField; private string cityNameField; private string postalZoneField; private AccountingSupplierPartyPartyPostalAddressCountry countryField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string StreetName { get { return this.streetNameField; } set { this.streetNameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string CityName { get { return this.cityNameField; } set { this.cityNameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string PostalZone { get { return this.postalZoneField; } set { this.postalZoneField = value; } } /// <remarks/> public AccountingSupplierPartyPartyPostalAddressCountry Country { get { return this.countryField; } set { this.countryField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyPartyPostalAddressCountry { private string identificationCodeField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string IdentificationCode { get { return this.identificationCodeField; } set { this.identificationCodeField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyPartyPartyTaxScheme { private string companyIDField; private AccountingSupplierPartyPartyPartyTaxSchemeTaxScheme taxSchemeField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string CompanyID { get { return this.companyIDField; } set { this.companyIDField = value; } } /// <remarks/> public AccountingSupplierPartyPartyPartyTaxSchemeTaxScheme TaxScheme { get { return this.taxSchemeField; } set { this.taxSchemeField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyPartyPartyTaxSchemeTaxScheme { private ID idField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public ID ID { get { return this.idField; } set { this.idField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyPartyPartyLegalEntity { private string companyIDField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string CompanyID { get { return this.companyIDField; } set { this.companyIDField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingSupplierPartyPartyContact { private string nameField; private string telephoneField; private object telefaxField; private string electronicMailField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string Name { get { return this.nameField; } set { this.nameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string Telephone { get { return this.telephoneField; } set { this.telephoneField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public object Telefax { get { return this.telefaxField; } set { this.telefaxField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string ElectronicMail { get { return this.electronicMailField; } set { this.electronicMailField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2", IsNullable = false)] public partial class AccountingCustomerParty { private AccountingCustomerPartyParty partyField; /// <remarks/> public AccountingCustomerPartyParty Party { get { return this.partyField; } set { this.partyField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingCustomerPartyParty { private AccountingCustomerPartyPartyPartyName partyNameField; private AccountingCustomerPartyPartyPostalAddress postalAddressField; private AccountingCustomerPartyPartyPartyTaxScheme partyTaxSchemeField; private AccountingCustomerPartyPartyContact contactField; /// <remarks/> public AccountingCustomerPartyPartyPartyName PartyName { get { return this.partyNameField; } set { this.partyNameField = value; } } /// <remarks/> public AccountingCustomerPartyPartyPostalAddress PostalAddress { get { return this.postalAddressField; } set { this.postalAddressField = value; } } /// <remarks/> public AccountingCustomerPartyPartyPartyTaxScheme PartyTaxScheme { get { return this.partyTaxSchemeField; } set { this.partyTaxSchemeField = value; } } /// <remarks/> public AccountingCustomerPartyPartyContact Contact { get { return this.contactField; } set { this.contactField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingCustomerPartyPartyPartyName { private string nameField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string Name { get { return this.nameField; } set { this.nameField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingCustomerPartyPartyPostalAddress { private string streetNameField; private string cityNameField; private string postalZoneField; private AccountingCustomerPartyPartyPostalAddressCountry countryField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string StreetName { get { return this.streetNameField; } set { this.streetNameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string CityName { get { return this.cityNameField; } set { this.cityNameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string PostalZone { get { return this.postalZoneField; } set { this.postalZoneField = value; } } /// <remarks/> public AccountingCustomerPartyPartyPostalAddressCountry Country { get { return this.countryField; } set { this.countryField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingCustomerPartyPartyPostalAddressCountry { private string identificationCodeField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public string IdentificationCode { get { return this.identificationCodeField; } set { this.identificationCodeField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingCustomerPartyPartyPartyTaxScheme { private AccountingCustomerPartyPartyPartyTaxSchemeTaxScheme taxSchemeField; /// <remarks/> public AccountingCustomerPartyPartyPartyTaxSchemeTaxScheme TaxScheme { get { return this.taxSchemeField; } set { this.taxSchemeField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")] public partial class AccountingCustomerPartyPartyPartyTaxSchemeTaxScheme { private ID idField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")] public ID ID { get { return this.idField; } set { this.idField = value; } } } /// <remarks/>
- List item using System;
-
Petrus 【KIM】 546 Reputation points
2021-11-23T08:42:24.597+00:00 I think you must check the xsd class named "Invoice". Try to make XSD class with "xsd.exe".
// Serialize/Deserialize public T DoXmlSerialization<T, T1>(T1 obj) { XmlSerializer xmlSer1 = null; XmlSerializer xmlSer2 = null; StringBuilder sb = null; StringWriter sw = null; StringReader sr = null; T oRetVal = default(T); try { if (obj == null) { throw new ArgumentNullException("obj"); } sb = new StringBuilder(); using (sw = new StringWriter(sb)) { xmlSer1 = new XmlSerializer(typeof(T1)); xmlSer1.Serialize(sw, obj); sw.Close(); } using (sr = new StringReader(sb.ToString())) { xmlSer2 = new XmlSerializer(typeof(T)); oRetVal = (T)xmlSer2.Deserialize(sr); sr.Close(); } } catch (Exception ex) { throw ex; } return oRetVal; } // XSD CLASS [System.SerializableAttribute()] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] public partial class MyXsd { private DateTime date; [System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")] public DateTime DATE { get { return date; } set { date = value; } } } // To Use MyXsd oxsd = new MyXsd(); oxsd.DATE = DateTime.Now; XmlDocument oXdoc = DoXmlSerialization<XmlDocument, MyXsd>(oxsd); XmlElement oElem = DoXmlSerialization<XmlElement, MyXsd>(oxsd); MyXsd oMyXsd = DoXmlSerialization<MyXsd, XmlElement>(oElem);