I'm currently developing a service for generating XML files which follows the UBL standard for an invoice, and therefore I need to use some provided XSD schemas.
I'm developing in .NET C#, and found a way where you can map the XSD to C# classes - by using the XSD.exe - which seems OK?
The problem that I'm facing is, that there are other namespaces in xsd file, which seems to make some problems for my generated classes (xsd:imports):
<!-- ===== xsd:schema Element With Namespaces Declarations ===== -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
xmlns:ccts="urn:un:unece:uncefact:documentation:2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
version="2.0">
<!-- ===== Imports ===== -->
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" schemaLocation="../common/UBL-CommonAggregateComponents-2.0.xsd"/>
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" schemaLocation="../common/UBL-CommonBasicComponents-2.0.xsd"/>
<xsd:import namespace="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" schemaLocation="../common/UnqualifiedDataTypeSchemaModule-2.0.xsd"/>
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" schemaLocation="../common/UBL-CommonExtensionComponents-2.0.xsd"/>
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" schemaLocation="../common/UBL-QualifiedDatatypes-2.0.xsd"/>
<!-- ===== Root Element ===== -->
<xsd:element name="Invoice" type="InvoiceType">
...
I ran the xsd.exe with the following command:
xsd.exe /c C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\maindoc\UBL-Invoice-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-CommonAggregateComponents-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-CommonBasicComponents-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UnqualifiedDataTypeSchemaModule-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-CommonExtensionComponents-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\UBL-QualifiedDatatypes-2.0.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_UnitCode_UNECE_7_04.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_MIMEMediaTypeCode_IANA_7_04.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_LanguageCode_ISO_7_04.xsd C:\Users\tn\Downloads\os-UBL-2.0\os-UBL-2.0\xsd\common\CodeList_CurrencyCode_ISO_7_04.xsd
When I try to make the XML file with use of the generated code, the output looks like this, where the imports and namespacepreceders is missing, and it ends up failing the validation.
<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<UBLVersionID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2.0</UBLVersionID>
<LegalMonetaryTotal xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<LineExtensionAmount currencyID="DKK" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">200</LineExtensionAmount>
<PayableAmount currencyID="DKK" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">300</PayableAmount>
</LegalMonetaryTotal>
</Invoice>
And here som samplecode where I generate the XML from the generated classes via a xmlserializer
XmlSerializer mySerializer = new XmlSerializer(typeof(InvoiceType));
InvoiceType invoice = new InvoiceType();
UBLVersionIDType UVer = new UBLVersionIDType();
UVer.Value = "2.0";
invoice.UBLVersionID = UVer;
MonetaryTotalType mtt = new MonetaryTotalType();
LineExtensionAmountType lep = new LineExtensionAmountType();
lep.currencyID = CurrencyCodeContentType.DKK;
lep.Value = 200;
PayableAmountType pat = new PayableAmountType();
pat.currencyID = CurrencyCodeContentType.DKK;
pat.Value = 300;
mtt.LineExtensionAmount = lep;
mtt.PayableAmount = pat;
invoice.LegalMonetaryTotal = mtt;
StreamWriter sw = new StreamWriter(@"C:\New folder\test2.xml");
mySerializer.Serialize(sw, invoice);
sw.Close();
How can I fix this, and is this the right (best/easiest( way to make XMLs which follows xsd schemas in .NET?
Found out myself.
Needed the XmlSerializerNamespaces-object which contains the namespaces and prefixes for the XML-document.
(http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With