【问题标题】:Xml generated from XSD class, dont have imports attributes and preceders namespaces从 XSD 类生成的 XML,没有导入属性和先行命名空间
【发布时间】:2016-04-22 17:20:28
【问题描述】:

这是我正在运行的命令:

E:\Invoice\maindoc>xsd.exe /c CurrencyCode.xsd LanguageCode.xsd MIMEMediaTypeCode.xsd UnitCode.xsd UBL-CommonAggregat   eComponents-2.0.xsd UBL-CommonBasicComponents-2.0.xsd   UnqualifiedDataTypeSchemaModule-2.0.xsd UBL-CommonExtensionComponents-2.0.xsd UBL-QualifiedDatatypes-2.0.xsd UBL-Invoice-2.0.xsd

我生成了 CS 类。这是部分测试源码

var invoice = new InvoiceType();

invoice.IssueDate = new IssueDateType();
invoice.IssueDate.Value = DateTime.Now;
var supplier = new SupplierPartyType();            

supplier.CustomerAssignedAccountID = new CustomerAssignedAccountIDType();
supplier.CustomerAssignedAccountID.Value = "5461564646";            

supplier.AdditionalAccountID = new AdditionalAccountIDType[1];
supplier.AdditionalAccountID[0] = new AdditionalAccountIDType
{
    Value = "6"
};

supplier.Party = new PartyType();
supplier.Party.PartyName = new PartyNameType[1];
supplier.Party.PartyName[0] = new PartyNameType
{
    Name = new NameType1 { Value = "EMPRESA X" }
};

invoice.AccountingSupplierParty = supplier;

string fichero = @"E:\InvoiceXsd\pruebas.xml";
XmlSerializer serializer = new XmlSerializer(typeof(InvoiceType));
FileStream fs = new FileStream(fichero, FileMode.Create);
serializer.Serialize(fs, invoice);
fs.Close();

生成的“pruebas.xml”部分是这样的:

<?xml version="1.0"?>
<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">

<ID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">F001-10</ID>
<IssueDate xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2016-04-22</IssueDate>
<InvoiceTypeCode xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">01</InvoiceTypeCode>
<AccountingSupplierParty xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
     <CustomerAssignedAccountID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">20272874680</CustomerAssignedAccountID>
     <AdditionalAccountID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">6</AdditionalAccountID>
     <Party>
        <PartyName>
          <Name xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">nombre empresa</Name>
        </PartyName>
        <PostalAddress>
           <ID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">150114</ID>
           <StreetName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">DIRECCION</StreetName>
           <CitySubdivisionName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
           <CityName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">DEPARTAMENTO</CityName>
           <CountrySubentity xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">DISTRITO</CountrySubentity>
           <District xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">LUGAR</District>
           <Country>
               <IdentificationCode xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">ES</IdentificationCode>
           </Country>
        </PostalAddress>
        <PartyLegalEntity>
           <RegistrationName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">NOMBRE EMPRESA</RegistrationName>
        </PartyLegalEntity>
   </Party>
</AccountingSupplierParty>

在“发票”元素中没有从 xsd.exe 命令生成的导入。在所有元素中都没有命名空间 xsd。

结果应该是这样的。 :

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><Invoice 
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:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<cac:AccountingSupplierParty>
  <cbc:CustomerAssignedAccountID>20100454523</cbc:CustomerAssignedAccountID>
  <cbc:AdditionalAccountID>6</cbc:AdditionalAccountID>
  <cac:Party>
    <cac:PostalAddress>
      <cbc:ID>150111</cbc:ID>
      <cbc:StreetName>AV. LOS PRECURSORES #1245</cbc:StreetName>
      <cbc:CitySubdivisionName>URB. MIGUEL GRAU</cbc:CitySubdivisionName>
      <cbc:CityName>LIMA</cbc:CityName>
      <cbc:CountrySubentity>LIMA</cbc:CountrySubentity>
      <cbc:District>EL AGUSTINO</cbc:District>
      <cac:Country>
      <cbc:IdentificationCode>PE</cbc:IdentificationCode>
      </cac:Country>
      </cac:PostalAddress>
      <cac:PartyLegalEntity>
      <cbc:RegistrationName>SOPORTE TECNOLOGICOS EIRL</cbc:RegistrationName>
    </cac:PartyLegalEntity>
  </cac:Party>
</cac:AccountingSupplierParty>

我做错了什么?任何帮助将不胜感激。

如何在节点中获取 cbc、cac、... 命名空间优先级?

【问题讨论】:

    标签: c# .net xml xsd


    【解决方案1】:
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
    

    ...

    serializer.Serialize(fs, invoice, ns);
    

    【讨论】:

    • 您好,欢迎来到 SO。请edit您添加代码格式的答案(只需删除不需要的空格,然后选择代码并按 ctrl-k),当您使用它时,还请添加一些描述或评论来解释这是什么以及为什么/它是如何解决问题的。谢谢!
    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 2012-06-01
    • 2021-05-04
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多