从XSD类生成的Xml,没有导入属性和先行命名空间

时间:2016-04-22 17:20:29

标签: c# .net xml xsd

这是我正在运行的命令:

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>

在元素“Invoice”中没有从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,...命名空间先行?

1 个答案:

答案 0 :(得分:1)

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");

...

serializer.Serialize(fs, invoice, ns);