Remove from: xml generated by my web service

2

When you generate XML in a web service rest, it looks 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>
    
asked by anonymous 23.05.2014 / 22:38

3 answers

0

I solved it like this:

BodyStyle = WebMessageBodyStyle.Bare

It used to be this way:

BodyStyle = WebMessageBodyStyle.Wrapped

    
29.05.2014 / 20:29
3

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

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

But I would advise you to read xml by a specialized class XmlReader or similar, (explanation here: link ) and just replace the names to avoid problems with the "body" of the file.

    
23.05.2014 / 23:03
3

The prefix a: before these elements declares that they belong to a namespace . This is not part of the element name. You can not 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 also have to change the xmlns:a statement that is in <getCnpjParceiroResult> . This statement associates all descending elements that have prefix a belonging to the "http://schemas.datacontract.org/2004/07/V99SuporteTecnicoContracts" namespace. Since there are no no elements, the prefix a of the descendants of <getCnpjParceiroResult> , you can set this namespace to default , which allows elements to be declared without a prefix .

So, in order for your document to remain valid and do not have the a prefixes, the namespace must be declared using xmlns (no suffix):

<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 XML generation, you need to figure out how to configure this in the software that generates it. There should be some map or registry where you can associate prefixes with namespaces, or choose preferences. However, there is no difference in functional terms for tags with or without prefix, unless you are using technologies that do not support namespaces. The two XMLs, your prefixed with a , what I posted above without the prefix, or another declaring a different prefix (ex: <datacontract:cnpj> ) will be equivalent and equally valid if the association is for the same namespace. / p>     

24.05.2014 / 00:09