Error when serializing with namespace in if

0

I was serializing a file and it worked normally, but now, when serializing q1 appears in all tags, I'm doing this:

StringWriter sw = new StringWriter();
        XmlTextWriter tw = new XmlTextWriter(sw);

        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();

        XmlSerializer ser = new XmlSerializer(typeof(GerarNfseEnvio));
        FileStream arquivo = new FileStream("E:\NFSe-" + "RPS" + numero.ToString().PadLeft(15, '0') + ".xml", FileMode.CreateNew);
        if (item.CodigoMunicipio == 3107109)
        {
            xsn.Add("", "http://www.betha.com.br/e-nota-contribuinte-ws");
        }
        else
        {
            xsn.Add("", "http://www.abrasf.org.br/nfse.xsd");
        }
        ser.Serialize(arquivo, gerar, xsn);
        arquivo.Close();

It worked perfectly, but now the tags appear like this:

<q1:GerarNfseEnvio xmlns="http://www.betha.com.br/e-nota-contribuinte-ws" xmlns:q1="http://www.abrasf.org.br/nfse.xsd">

It's adding the two, only when I leave only the xsn.Add("", "http://www.abrasf.org.br/nfse.xsd"); line that works. It was working perfectly. After adding the if statement, it was this way. How to correct?

    
asked by anonymous 12.12.2018 / 12:05

1 answer

1

You can save the second parameter in a variable. It should work this way:

    StringWriter sw = new StringWriter();
    XmlTextWriter tw = new XmlTextWriter(sw);

    XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();

    XmlSerializer ser = new XmlSerializer(typeof(GerarNfseEnvio));
    FileStream arquivo = new FileStream("E:\NFSe-" + "RPS" + numero.ToString().PadLeft(15, '0') + ".xml", FileMode.CreateNew);
    String url = "http://www.abrasf.org.br/nfse.xsd";
    if (item.CodigoMunicipio == 3107109)
    {
        url = "http://www.betha.com.br/e-nota-contribuinte-ws";
    }
    xsn.Add("", url);

    ser.Serialize(arquivo, gerar, xsn);
    arquivo.Close();
    
12.12.2018 / 12:20