使用带名称空间的ChildNodes从XML读取值

时间:2022-10-26 17:02:41

I have 2 XML files, first this one which works just fine:

我有2个XML文件,首先这个工作得很好:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:ConsultarSituacaoLoteRpsResposta xmlns:ns2="http://www.ginfes.com.br/tipos_v03.xsd" xmlns:ns3="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd">
  <ListaMensagemRetorno>
    <ns2:MensagemRetorno>
      <ns2:Codigo>E172</ns2:Codigo>
    </ns2:MensagemRetorno>
  </ListaMensagemRetorno>
</ns3:ConsultarSituacaoLoteRpsResposta>

The code I'm using to read it is something like this:

我用来读它的代码是这样的:

MyNode := Doc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('MensagemRetorno');
MyValue := MyNode.ChildValues['Codigo'];

The problem is that I have a second XML:

问题是我有第二个XML:

<?xml version="1.0" encoding="utf-8"?>
<ConsultarSituacaoLoteRpsResposta xmlns="http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_consultar_situacao_lote_rps_resposta.xsd">
  <ListaMensagemRetorno>
    <MensagemRetorno>
        <Codigo xmlns="http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd">E156</Codigo>
    </MensagemRetorno>
  </ListaMensagemRetorno>
</ConsultarSituacaoLoteRpsResposta>

Notice that this XML have a namespace in the "Codigo" node, so my code doesn't find this node.

请注意,此XML在“Codigo”节点中具有命名空间,因此我的代码找不到此节点。

The only way I found to read the "Codigo" value from this second XML was like this:

我发现从第二个XML读取“Codigo”值的唯一方法是这样的:

for I := 0 to MyNode.ChildNodes.Count -1 do
begin
  ChildNode := RetornoNode.ChildNodes[I];
  if ChildNode.NodeName = 'Codigo' then
    Codigo := ChildNode.NodeValue;
end;

But I think that there should be a better way to do this, as I still didn't understand why the first code doesn't work with the second XML.

但我认为应该有更好的方法来做到这一点,因为我仍然不明白为什么第一个代码不适用于第二个XML。

Anyone could please clarify it for me?

有人可以帮我澄清一下吗?

1 个解决方案

#1


5  

This it seems a limitation of the ChildValues property

这似乎是ChildValues属性的限制

You can use one of these alternatives to return the value

您可以使用其中一个替代值来返回值

 MyNode := Doc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('MensagemRetorno');
 MyValue :=MyNode.ChildNodes.First.Text;

or using the overloaded version of the FindNode method

或使用FindNode方法的重载版本

 MyNode := Doc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('MensagemRetorno');
 MyValue:= MyNode.ChildNodes.FindNode('Codigo','http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd').Text;

#1


5  

This it seems a limitation of the ChildValues property

这似乎是ChildValues属性的限制

You can use one of these alternatives to return the value

您可以使用其中一个替代值来返回值

 MyNode := Doc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('MensagemRetorno');
 MyValue :=MyNode.ChildNodes.First.Text;

or using the overloaded version of the FindNode method

或使用FindNode方法的重载版本

 MyNode := Doc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('MensagemRetorno');
 MyValue:= MyNode.ChildNodes.FindNode('Codigo','http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd').Text;