如何为Xml Root和XmlElement添加相同的名称空间?当我执行XmlElement时,不显示此名称空间

时间:2019-03-21 17:41:49

标签: c# serialization xml-serialization xml-namespaces xmlwriter

我正在尝试使用XmlSerialize创建xml。

DTO.XML xml = new DTO.XML();
                xml.versao = mdfe.VersaoLayout;

                xml.MDFe = new DTO.MDFe();  

                xml.MDFe.InfMDFe = new DTO.InfMDFe();
                xml.MDFe.InfMDFe.versao = mdfe.VersaoLayout;
                xml.MDFe.InfMDFe.Id = idMontado;
                xml.MDFe.InfMDFe.Ide = new DTO.Ide();

                xml.MDFe.InfMDFe.Ide.cUF = mdfe.Empresa.Estado.CodIBGE;
                xml.MDFe.InfMDFe.Ide.tpAmb = mdfe.TipoAmbiente;
                xml.MDFe.InfMDFe.Ide.tpEmit = mdfe.TipoEmitente;
                xml.MDFe.InfMDFe.Ide.mod = mdfe.ModeloMdfe;
                xml.MDFe.InfMDFe.Ide.serie = mdfe.Serie ;
                xml.MDFe.InfMDFe.Ide.nMDF = mdfe.Numero;
                xml.MDFe.InfMDFe.Ide.cMDF = mdfe.CodNumerico;
                xml.MDFe.InfMDFe.Ide.cDV = mdfe.Verificador;
                xml.MDFe.InfMDFe.Ide.modal = mdfe.TipoModal;
                xml.MDFe.InfMDFe.Ide.dhEmi = mdfe.DataHoraEmissao;
                xml.MDFe.InfMDFe.Ide.tpEmis = mdfe.FormaEmissao;
                xml.MDFe.InfMDFe.Ide.procEmi = mdfe.ProcessoEmissao;
                xml.MDFe.InfMDFe.Ide.verProc = mdfe.VersaoProcesso;
                xml.MDFe.InfMDFe.Ide.UFIni = mdfe.UFEstadoCarregamento.ToString() ;
                xml.MDFe.InfMDFe.Ide.UFFim = mdfe.UFEstadoDescarregamento.ToString();

                XmlSerializerNamespaces names = new XmlSerializerNamespaces();
                names.Add("", "http://www.portalfiscal.inf.br/mdfe");
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                XmlWriter arquivo = XmlWriter.Create("C:\\teste\\MDFe.xml", settings);
                XmlSerializer serializer = new XmlSerializer(xml.GetType());                                  
                serializer.Serialize(arquivo, xml, names);
                arquivo.Close();

我的控制器:

<?xml version="1.0" encoding="utf-8"?>
<mdfeProc versao="3.00" xmlns="http://www.portalfiscal.inf.br/mdfe">
  <MDFe>
    <infMDFe Id="MDFe3319020473845500016058111123456789" versao="3.00">
      <ide>
        <cUF>33</cUF>
        <tpAmb>2</tpAmb>
        <tpEmit>2</tpEmit>
        <mod>58</mod>
        <serie>1</serie>
        <nMDF>1</nMDF>
        <cMDF>12345678</cMDF>
        <cDV>9</cDV>
        <modal>1</modal>
        <dhEmi>2019-02-11T10:29:00</dhEmi>
        <tpEmis>1</tpEmis>
        <procEmi>0</procEmi>
        <verProc>1.00</verProc>
        <UFIni>19</UFIni>
        <UFFim>19</UFFim>
      </ide>
    </infMDFe>
  </MDFe>
</mdfeProc>

我的文件结果:

<?xml version="1.0" encoding="utf-8"?>
<mdfeProc versao="3.00" xmlns="http://www.portalfiscal.inf.br/mdfe">
  <MDFe xmlns="http://www.portalfiscal.inf.br/mdfe">
    <infMDFe Id="MDFe3319020473845500016058111123456789" versao="3.00">
      <ide>
        <cUF>33</cUF>
        <tpAmb>2</tpAmb>
        <tpEmit>2</tpEmit>
        <mod>58</mod>
        <serie>1</serie>
        <nMDF>1</nMDF>
        <cMDF>12345678</cMDF>
        <cDV>9</cDV>
        <modal>1</modal>
        <dhEmi>2019-02-11T10:29:00</dhEmi>
        <tpEmis>1</tpEmis>
        <procEmi>0</procEmi>
        <verProc>1.00</verProc>
        <UFIni>19</UFIni>
        <UFFim>19</UFFim>
      </ide>
    </infMDFe>
  </MDFe>
</mdfeProc>

但是,我需要这个结果:

<MDFe>

当我在工作中更改声明的名称空间时,如何使该名称空间在标签{{1}}中工作?

0 个答案:

没有答案