Dirt in XML string, line break( n) sent whenever the field is empty!

Asked

Viewed 262 times

0

I’m assembling a string that has as its final result an XML template just below, and when receiving at the other end, a line break character appears in the empty fields, I’ve tried using replace(), regex... someone has a suggestion?

StringBuilder Sb = new StringBuilder();

Sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
Sb.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://dominio.com.br/vv/operacao_lojas/optout/atualizar/SOAP/v1\">";
Sb.Append("<soapenv:Header/>");
Sb.Append("<soapenv:Body>");
Sb.Append("<v1:atualizarOptOut>");
Sb.Append("<codigoClienteCpf>{0}</codigoClienteCpf>");
Sb.Append("<codigoProduto>{1}</codigoProduto>");
Sb.Append("<statusEnvioEmail>{2}</statusEnvioEmail>");
Sb.Append("<statusEnvioSms>{3}</statusEnvioSms>");
Sb.Append("<descricaoEmailCliente>{4}</descricaoEmailCliente>");
Sb.Append("<numeroDddCelCliente>{5}</numeroDddCelCliente>");
Sb.Append("<numeroCelularCliente>{6}</numeroCelularCliente>");
Sb.Append("</v1:atualizarOptOut>");
Sb.Append("</soapenv:Body>");
Sb.Append("</soapenv:Envelope>");

var retorno = String.Format(Sb.ToString(),
obj.codigoClienteCpf,
obj.codigoProduto,
**CleanInput(obj.statusEnvioEmail.Trim()),
CleanInput(obj.statusEnvioSms.Trim()),
CleanInput(obj.descricaoEmailCliente.Trim())
CleanInput(obj.numeroDddCelCliente.Trim()),
CleanInput(obj.numeroCelularCliente.Trim())**).Replace("\n","");


private static string CleanInput(string strIn)
{
//Remove todos os caracteres não alphanuméricos exceto (.) pontos, (@) arrobas e (-) hífens
try
{
 return Regex.Replace(strIn, @"[^\w\.@-]", "",
                 RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
catch (RegexMatchTimeoutException)
{

return String.Empty;
 }
}
  • You’ve tried it this way CleanInput(obj.numeroCelularCliente.Trim())).Replace(Environment.NewLine, string.Empty);

  • Hello John, thank you so much for your attention. I just tested your suggestion, but it didn’t work.

  • How do you know it’s an " n" he’s getting?

  • @Lucianocastilho, if you have a method CleanInput(), you should improve it to already perform this treatment, as well as verify the reason for receiving these incorrect values in your inputs.

  • @Joãomartins , I know why I have access next to the mainframe, and there he receives the Soap with this information of n.

  • @Leandroangelo is actually right, but it is necessary that I return a value or even the empty field in the statusSend and that is what I do. I thought it might be something related to encoding, but as in xml I already inform utf-8, and in the webrequest header I add the content-type also to utf-8... I didn’t know what to do.

  • @Lucianocastilho can put an example of the result? does ' n' appear on all lines in all empty spaces or only at the end of xml? How does the other end receive this xml? What is the code?

  • @Guilhermebatista, cara o " n" arrives at the final destination in the empty fields. The other end is a Mainframe, Cics controls the reception and feedback of information.

  • @Lucianocastilho When you say "empty fields" refers for example to the field <codigoClienteCpf>{0}</codigoClienteCpf>? If there is no value for {0} it puts n ? that’s it?

  • @Guilhermebatista, then if there is no value to be placed it replaces by empty. value = "". but when it arrives at the server.... is there the n.

  • @Lucianocastilho now understood the problem. Is this not a character encoding error? It may be that the coding of your program is different from the recipient.

  • @Guilhermebatista are you saying something like encoding? I’m sending the encoding in Webrequest Req.Contenttype = "text/xml;charset="utf-8"";

  • @Lucianocastilho that’s right. Your code seems to be correct. try modifying the encoding and see if it works.

  • @Guilhermebatista, so I tried with ISO-8859-1, and nothing!

  • @Lucianocastilho complicated this situation. Well, the only option I see for now is to try to remove the line break individually for each field and see if it works.

  • @Guilhermebatista, so I’ve done it... . Replace(" n",""), individually and in the total string

  • Good morning Friends!! I couldn’t really solve this little problem, so to not stop my workflow I did other validations, but I couldn’t figure out what the problem might be. Thank you for your attention.

Show 12 more comments
No answers

Browser other questions tagged

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