C# 无法找到通过文本从XMLNodeList获取子节点的方法

C# 无法找到通过文本从XMLNodeList获取子节点的方法,c#,xmldom,xmlnodelist,C#,Xmldom,Xmlnodelist,这让我很痛苦,我有一个XMLDocument,我需要能够获取某些节点的文本值,但我尝试的一切都失败了。给定下面的XML文档,我需要获取患者ID、姓氏等信息 <Message xmlns="http://" version="010" release="006"> <Header> <To Qualifier="C">1306841101</To> <From Qualifier="P">8899922</From&

这让我很痛苦,我有一个XMLDocument,我需要能够获取某些节点的文本值,但我尝试的一切都失败了。给定下面的XML文档,我需要获取患者ID、姓氏等信息

<Message xmlns="http://" version="010" release="006">
<Header>
    <To Qualifier="C">1306841101</To>
    <From Qualifier="P">8899922</From>
</Header>
<Body>
    <RxFill>
        <Patient>
            <Identification>
      <ID>193306093523</ID>
      <ID3>111223333</ID3>
            </Identification>
            <Name>
                <LastName>Smith</LastName>
                <FirstName>Jane</FirstName>
            </Name>
        </Patient>
    </RxFill>
</Body>
但是,如果我尝试获取一个子节点,xpath将永远找不到它们

例如:

oDoc.GetElementsByTagName("Patient")[0].SelectSingleNode("Identification")
即使我可以在调试器中看到“Identification”是第一个子项,也会显示为null。我还添加了斜杠://Identification,没有欢乐

但是,我可以从文件中了解到:

oDoc.GetElementsByTagName("Identification")
但这不起作用,因为我可能在文档中有其他类似的标记;我只想要病人的识别标签

我试着在所有的孩子中寻找他们,但这似乎效率很低


有什么想法吗?

您应该在XPath中包含名称空间。您可以使用
XmlNamespaceManager
执行此操作:

XmlNode root = oDoc.DocumentElement;
XmlNode patient = oDoc.GetElementsByTagName("Patient")[0];

XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("ns", "http://");

XmlNode identification = patient.SelectSingleNode("ns:Identification", nsm);
string id = identification.SelectSingleNode("ns:ID", nsm).InnerText;

该死的,就是这样,谢谢。我想我需要研究名称空间为什么会影响xpath:()
XmlNode root = oDoc.DocumentElement;
XmlNode patient = oDoc.GetElementsByTagName("Patient")[0];

XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("ns", "http://");

XmlNode identification = patient.SelectSingleNode("ns:Identification", nsm);
string id = identification.SelectSingleNode("ns:ID", nsm).InnerText;