Tôi đang làm việc trong chương trình tính thuế Mexico và chính phủ cung cấp tệp XSD sau http://www.sat.gob.mx/cfd/3/cfdv32.xsd, với trợ giúp xsd.exe tôi đã chuyển đổi nó thành lớp C# khá lớn nên tôi ' sẽ chỉ cung cấp một liên kết đến nó để không bloat bài đăng này với mã số: http://pastebin.com/r3VCgFMU.Thêm tiền tố và không gian tên vào tuần tự hóa XML
Sau khi điền MỘT SỐ của các trường lớp (Vì vậy, ví dụ không nên quá lớn) Tôi cố gắng để serialize XML như sau:
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();
nào mang lại cho tôi kết quả này:
<?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>
Như chúng ta có thể thấy trong ví dụ được cung cấp bởi chính phủ (ftp://ftp2.sat.gob.mx/asistencia_servicio_ftp/publicaciones/solcedi/ejemplo1%20cfdv3.xml) tệp đã tạo của tôi thiếu một số điểm:
<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
Tôi có thể đạt được những thay đổi này trong xml của mình bằng cách nào? : D
bạn không cần phải thực hiện bất kỳ thay đổi. hai tài liệu giống hệt nhau. Xin xem nếu http://stackoverflow.com/tags/xml-namespaces/info giúp bạn ở tất cả Nếu nó không giúp được thì hãy nói như vậy. –