Я пытаюсь создать XML с помощью XmlSerialize.
[XmlRoot(ElementName = "mdfeProc",Namespace = "http://www.portalfiscal.inf.br/mdfe", IsNullable = false)]
public class XML
{
[XmlElement(ElementName = "MDFe", Namespace = "http://www.portalfiscal.inf.br/mdfe")]
public MDFe MDFe { get; set; }
[XmlAttribute(AttributeName = "versao")]
public string versao { get; set; }
}
public class MDFe
{
[XmlElement(ElementName = "infMDFe", Namespace = "http://www.portalfiscal.inf.br/mdfe")]
public InfMDFe InfMDFe { get; set; }
}
[XmlRoot(ElementName = "infMDFe") ]
public class InfMDFe
{
[XmlElement(ElementName = "ide", Namespace = "http://www.portalfiscal.inf.br/mdfe")]
public Ide Ide { get; set; }
[XmlAttribute(AttributeName = "Id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "versao")]
public string versao { get; set; }
}
[XmlRoot(ElementName = "ide", Namespace = "http://www.portalfiscal.inf.br/mdfe")]
public class Ide
{
[XmlElement(ElementName = "cUF", Namespace = "http://www.portalfiscal.inf.br/mdfe")]
public int cUF { get; set; }
}
Мой контроллер:
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>
?