6
我正在墨西哥稅務計算程序中工作,政府提供了以下XSD文件http://www.sat.gob.mx/cfd/3/cfdv32.xsd,它帶有xsd.exe幫助我已將其轉換爲C#類,它非常大,只需提供一個鏈接,以便不會使用代碼:http://pastebin.com/r3VCgFMU膨脹此帖。向XML序列化添加前綴和名稱空間
填充一些類字段(這樣的例子並沒有得到太大)後,我試圖序列化XML如下:
XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
xmlNameSpace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlNameSpace.Add("schemaLocation", "http://www.sat.gob.mx/cfd/3/cfdv32.xsd");
xmlNameSpace.Add("cfdi", "www.sat.gob.mx/cfd/3");
XmlTextWriter xmlTextWriter = new XmlTextWriter("c:\\temp\\pruebas.xml", Encoding.UTF8);
xmlTextWriter.Formatting = Formatting.Indented;
XmlSerializer xs = new XmlSerializer(typeof(Comprobante));
xs.Serialize(xmlTextWriter, comprobante, xmlNameSpace);
xmlTextWriter.Close();
,給了我這樣的輸出:
<?xml version="1.0" encoding="utf-8"?>
<Comprobante xmlns:cfdi="www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="http://www.sat.gob.mx/cfd/3/cfdv32.xsd" version="3.2" fecha="0001-01-01T00:00:00" subTotal="0" total="0" tipoDeComprobante="ingreso" xmlns="http://www.sat.gob.mx/cfd/3">
<Emisor rfc="DERH9145202V4">
<DomicilioFiscal calle="Calle1" colonia="Colonia" municipio="municipio" estado="estado" pais="pais" codigoPostal="07000" />
<RegimenFiscal Regimen="Peque" />
</Emisor>
</Comprobante>
正如我們在政府(ftp://ftp2.sat.gob.mx/asistencia_servicio_ftp/publicaciones/solcedi/ejemplo1%20cfdv3.xml)提供了一個示例見我生成的文件缺少一些要點:
<Comprobante... should be <cfdi:Comprobante
<Emisor.. should be <cfdi:Emisor
and so on and so forth with all elements...
xmlns:schemaLocation should be xsi:schemaLocation
I'm getting and additional xmlns="http://www.sat.gob.mx/cfd/3" at the end of the Comprobante declaration
如何在我的xml中實現這種更改? :d
你不需要做任何改變。這兩個文件是相同的,請查看是否http://stackoverflow.com/tags/xml-namespaces/info可以幫助你在所有如果它沒有幫助,那麼請說出來。 –