Remove: from xml generated by my web service

Asked

Viewed 601 times

2

When I use XML in a Rest web service, it appears like this:

<getCnpjParceiroResponse xmlns="http://tempuri.org/">
    <getCnpjParceiroResult 
        xmlns:a="http://schemas.datacontract.org/2004/07/V99SuporteTecnicoContracts" 
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Bairro>SAO VICENTE</a:Bairro>
        <a:CEP i:nil="true"/>
        <a:CNPJ>11951604000130</a:CNPJ>
        <a:CaminhoLogo/>
        <a:Celular i:nil="true"/>
        <a:Cidade>PIRACICABA</a:Cidade>
        <a:Complemento i:nil="true"/>
        <a:DDD>0</a:DDD>
        <a:DDDCelular>0</a:DDDCelular>
        <a:DataAlteracao>0001-01-01T00:00:00</a:DataAlteracao>
        <a:DataCadastro>0001-01-01T00:00:00</a:DataCadastro>
        <a:Distrito i:nil="true"/>
        <a:Email i:nil="true"/>
        <a:Endereco>AV. CRISTOVAO COLOMBO</a:Endereco>
        <a:EnderecoIPInstalacao i:nil="true"/>
        <a:Estado i:nil="true"/>
        <a:IDPdv>0</a:IDPdv>
        <a:IDTipoEstabelecimento>0</a:IDTipoEstabelecimento>
        <a:IDTipoRede>0</a:IDTipoRede>
        <a:ID_Rede>0</a:ID_Rede>
        <a:IS_Ativo>false</a:IS_Ativo>
        <a:Latitude>0</a:Latitude>
        <a:Longitude>0</a:Longitude>
        <a:NomeFantasia i:nil="true"/>
        <a:NomeRede i:nil="true"/>
        <a:Numero i:nil="true"/>
        <a:QtdeCheckOuts>0</a:QtdeCheckOuts>
        <a:RazaoSocial>CONVENIENCIA RADIAL NORTE SUL LTDA EPP</a:RazaoSocial>
        <a:Telefone i:nil="true"/>
        <a:TokenAuthentication i:nil="true"/>
    </getCnpjParceiroResult>
</getCnpjParceiroResponse>
  • Note to editor: @helderdarocha, we should not never add or remove anything to the original code posted in a question as this may fix the error or introduce other problems. Hit indentation and remove blank lines is ok.

  • @brasofilo Nothing was introduced or removed from the original code. It was only endented. The only thing added was a tag, [tag:xml-namespaces], which is relevant to the question.

  • 1

    Now I saw @brasofilo. Sorry. I used a tool to copy the code and she introduced an XML statement. I hadn’t noticed. Anyway this does not interfere with the problem in question.

  • The original did not have the opening tag <xml../>. If the file is being generated without it, it is another problem...

3 answers

3

if you want a quick and practical way, use the Replace method of the String or Stringbuilder class (faster):

string xml = new StringBuilder(xmlContent);
xml.Replace("<a:", "<");
xml.Replace("</:a", "</");
xmlContent = xml.ToString();

but I advise you to read the xml by a specialized class XmlReader or similar, (explanation here: http://msdn.microsoft.com/en-us/library/vstudio/system.xml.xmlreader.read) and just replace the names to avoid problems with the "body" of the file.

  • If you are going to do this you would also have to remove the suffix of the statement xmlns:a, otherwise the elements will lose their association with the namespace and cannot be validated.

3

The prefix a: before those elements declares that they belong to a namespace. This is not part of the element name. You cannot simply remove them without making changes to other parts of the file or your document may be invalid.

If you really need to remove the prefixes, you will have to change the declaration as well xmlns:a who is in <getCnpjParceiroResult>. This statement associates all descending elements that have prefixes a belong to the namespace "http://schemas.datacontract.org/2004/07/V99SuporteTecnicoContracts". As there are no elements without the prefix a among the descendants of <getCnpjParceiroResult>, you can establish this namespace as the default, which allows elements to be declared without prefix.

So that your document remains valid and does not have prefixes a the namespace must be declared using xmlns (unfilled):

<getCnpjParceiroResponse xmlns="http://tempuri.org/">
    <getCnpjParceiroResult 
        xmlns="http://schemas.datacontract.org/2004/07/V99SuporteTecnicoContracts" 
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Bairro>SAO VICENTE</Bairro>
        <CEP i:nil="true"/>
        <CNPJ>11951604000130</CNPJ>
        <CaminhoLogo/>
        <Celular i:nil="true"/>
        ...
        <Telefone i:nil="true"/>
        <TokenAuthentication i:nil="true"/>
    </getCnpjParceiroResult>
</getCnpjParceiroResponse>

As for the generation of XML, you need to figure out how to configure this in the software that generates it. There must be some map or record where you can associate prefixes with namespaces, or choose preferences. Anyway, there is no difference in functional terms for prefixed or non-prefixed tags, unless you are using technologies that do not support namespaces. The two Xmls, yours with prefix a, what I posted above without the prefix, or yet another declaring a different prefix (ex: <datacontract:cnpj>) shall be equivalent and equally valid if the association is for the same namespace.

  • helderdarocha, good morning. I have this information after the document is generated. There is how before the generation of this document, I do something that prevents tag a? The problem that the partner company that will consume this web service, the programmer there said that this tag is giving problems. I could say that this is her problem and so on, but I like to smooth the way, so if I can fail on my side I will.

  • I don’t know if it’s trivial with WCF. I found this article. If it gets too complicated, you can write a prefix boot a: and the suffix of xmlns:a as proposed in the other answer, since there is no predefined default namespace.

0


I resolved so:

Bodystyle = Webmessagebodystyle.Bare

Before it was like this:

Bodystyle = Webmessagebodystyle.Wrapped

Browser other questions tagged

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