2012-03-08 44 views
4

我目前正在開發用於產生如下發票的UBL標準的XML文件的服務,因此,我需要使用一些提供XSD架構。XSD爲XML的進口

我在.NET C#開發,並找到了一種方法,你可以映射到XSD C#類 - 通過使用XSD.EXE - 這似乎好不好?

,我要面對的問題是,有在XSD文件,這似乎使一些問題我生成的類(XSD:進口):其他命名空間

<!-- ===== 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"> 
... 

我跑了XSD。用下面的命令exe文件:

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 

當我嘗試讓XML文件,使用生成的代碼,輸出看起來是這樣的,在進口和namespacepreceders丟失,並且它最終失敗的驗證。

<?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> 

這裏索姆samplecode,我通過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(); 

我怎樣才能解決這個生成生成的類的XML,這是正確的(最好/最簡單的(方法,使個XML隨後XSD架構在.NET?

回答

2

找到了自己。

需要XmlSerializerNamespaces對象包含的XML文檔的命名空間和前綴。

(http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx)

+0

你是怎麼做的就是CBC:等命名空間preceders出現..你是否必須修改UBL XSD輸出的c#類?或者當它序列化時它只是不同... – 2014-04-14 06:44:47

+0

@JamesReategui你找到了你的問題的答案? – MichaelD 2015-02-22 07:05:26