简体   繁体   中英

C# - Deserialize an XML file with multiple ns

I've been in trouble trying to deserialize the following XML below. I have the error "There is an error in XML document (1, 2)". Any tips? Thanks in advance.

<ns:obterControleDieselResponse>
    <ns:return type="br.com.framew2.webservices.posicoes.RetornoDiesel">
        <ax26:diesel type="br.com.framew2.webservices.posicoes.ControleDiesel">
            <ax26:bomba>B01PTS</ax26:bomba>
            <ax26:cliente>PTS TRANSPORTES</ax26:cliente>
            <ax26:combustivel>DIESEL</ax26:combustivel>
            <ax26:data_gps>2013-09-04T14:38:39.000Z</ax26:data_gps>
            <ax26:datainicioabastecimento>2013-09-04T14:29:59.000Z</ax26:datainicioabastecimento>
            <ax26:endereco>r aguilino limongi - 284 a 379 - itu - SP</ax26:endereco>
            <ax26:evento_data>2013-09-04T14:29:59.000Z</ax26:evento_data>
            <ax26:filial>PTS TRANSPORTES</ax26:filial>
            <ax26:fornecido>2013-09-04T14:29:59.000Z</ax26:fornecido>
            <ax26:frentista>RONALDO ORMONDE</ax26:frentista>
            <ax26:frentista_id>0</ax26:frentista_id
ax26:horimetro_anterior>0</ax26:horimetro_anterior>
        <ax26:horimetro_atual>9</ax26:horimetro_atual>
        <ax26:id>7702</ax26:id>
        <ax26:id_veiculo>53549</ax26:id_veiculo>
        <ax26:km_anterior>0</ax26:km_anterior>
        <ax26:km_atual>0</ax26:km_atual>
        <ax26:licenca>DAH4974</ax26:licenca>
        <ax26:litros>0.0</ax26:litros>
        <ax26:motorista/>
        <ax26:numero>3061353</ax26:numero>
        <ax26:numero_sequencia>0</ax26:numero_sequencia>
        <ax26:numeroidveiculo_str>9671765</ax26:numeroidveiculo_str>
        <ax26:placa>DAH4974</ax26:placa>
        <ax26:tanque>TQ1PTS</ax26:tanque>
        <ax26:tempo>520</ax26:tempo>
        <ax26:valor_arla>0.0</ax26:valor_arla>
        <ax26:valor_lubrificante>0.0</ax26:valor_lubrificante>
    </ax26:diesel>
</ns:return>
</ns:obterControleDieselResponse>

About half way down you have the following issue:

<ax26:frentista_id>0</ax26:frentista_id
ax26:horimetro_anterior>0</ax26:horimetro_anterior>

Both lines are missing angle brackets ( < and > )...

You need to declare the namespaces ns and ax26 like

<ns:obterControleDieselResponse xmlns:ns="http://x.y.z">
    <ns:return type="br.com.framew2.webservices.posicoes.RetornoDiesel" xmlns:ax26="http://a.b.c.d">
        <ax26:diesel type="br.com.framew2.webservices.posicoes.ControleDiesel">
            <ax26:bomba>B01PTS</ax26:bomba>
            <ax26:cliente>PTS TRANSPORTES</ax26:cliente>
            <ax26:combustivel>DIESEL</ax26:combustivel>
            <ax26:data_gps>2013-09-04T14:38:39.000Z</ax26:data_gps>
            <ax26:datainicioabastecimento>2013-09-04T14:29:59.000Z</ax26:datainicioabastecimento>
            <ax26:endereco>r aguilino limongi - 284 a 379 - itu - SP</ax26:endereco>
            <ax26:evento_data>2013-09-04T14:29:59.000Z</ax26:evento_data>
            <ax26:filial>PTS TRANSPORTES</ax26:filial>
            <ax26:fornecido>2013-09-04T14:29:59.000Z</ax26:fornecido>
            <ax26:frentista>RONALDO ORMONDE</ax26:frentista>
            <ax26:frentista_id>0</ax26:frentista_id>
            <ax26:horimetro_anterior>0</ax26:horimetro_anterior>
            <ax26:horimetro_atual>9</ax26:horimetro_atual>
            <ax26:id>7702</ax26:id>
            <ax26:id_veiculo>53549</ax26:id_veiculo>
            <ax26:km_anterior>0</ax26:km_anterior>
            <ax26:km_atual>0</ax26:km_atual>
            <ax26:licenca>DAH4974</ax26:licenca>
            <ax26:litros>0.0</ax26:litros>
            <ax26:motorista/>
            <ax26:numero>3061353</ax26:numero>
            <ax26:numero_sequencia>0</ax26:numero_sequencia>
            <ax26:numeroidveiculo_str>9671765</ax26:numeroidveiculo_str>
            <ax26:placa>DAH4974</ax26:placa>
            <ax26:tanque>TQ1PTS</ax26:tanque>
            <ax26:tempo>520</ax26:tempo>
            <ax26:valor_arla>0.0</ax26:valor_arla>
            <ax26:valor_lubrificante>0.0</ax26:valor_lubrificante>
        </ax26:diesel>
    </ns:return>
</ns:obterControleDieselResponse>

Edit:

To declare the namespaces inside your code you could try using the XmlNamespaceManager like

var xmlDoc = new XmlDocument();
xmlDoc.Load(PathToFile);

var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://x.y.z");
nsmgr.AddNamespace("ax26", "http://a.b.c.d");

var node = doc.SelectSingleNode("//ns:obterControleDieselResponse", nsmgr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM