Error when serializing with namespace in if

Asked

Viewed 54 times

0

I was creating a file and it worked normal, but now, when serialize appears Q1 in all tags, I’m doing 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);
        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">

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

1 answer

1


You can save the second parameter to 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();
  • Doesn’t solve it, keeps popping up the same way <q1:GerarNfseEnvio xmlns="http://www.betha.com.br/e-nota-contribuinte-ws" xmlns:q1="http://www.abrasf.org.br/nfse.xsd"> as if I didn’t have the if

  • Ok, I couldn’t comment before because I don’t have enough points. What you can try is to use the default namespace as second constructor parameter and add the betha.com.br.... with the xsn.Addin the if

Browser other questions tagged

You are not signed in. Login or sign up in order to post.