C# - 使用多个ns反序列化XML文件

时间:2015-12-22 12:53:25

标签: c# xml

我在尝试反序列化以下XML时遇到了麻烦。我有错误" XML文档(1,2)和#34;中存在错误。有小费吗?提前谢谢。

<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>

2 个答案:

答案 0 :(得分:1)

大约一半的时间你有以下问题:

.dropdown ul {
  list-style-type: none;
  visibility: hidden;
  position: absolute;
  top: 100%;
  left: 0;
  color: red;
  width: 100%;
  opacity: 0;
  background: yellow;
  border: 1px solid black;
  transition: visibility 1s, opacity 1s;
}

.dropdown:hover ul {
  opacity: 1;
  visibility: visible;
}

这两行都缺少尖括号(<ax26:frentista_id>0</ax26:frentista_id ax26:horimetro_anterior>0</ax26:horimetro_anterior> <)...

答案 1 :(得分:0)

您需要声明名称空间nsax26

<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);