C#-反序列化具有多个ns的XML文件
本文关键字:ns XML 文件 反序列化 C#- | 更新日期: 2023-09-27 18:27:53
我在尝试反序列化下面的XML时遇到了麻烦。我有错误"XML文档(1,2)中有错误"。有什么建议吗?提前谢谢。
<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>
大约进行到一半时,出现以下问题:
<ax26:frentista_id>0</ax26:frentista_id
ax26:horimetro_anterior>0</ax26:horimetro_anterior>
两行都缺少尖括号(<
和>
)。。。
您需要像一样声明名称空间ns
和ax26
<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>
编辑:
要在代码中声明命名空间,可以尝试使用类似的XmlNamespaceManager
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);