C# 基于同级选择树中的节点

C# 基于同级选择树中的节点,c#,xml,xpath,C#,Xml,Xpath,我在解析节点树时遇到了一个问题。下面是我正在使用的XML文档示例: <result> <type>street_address</type> <formatted_address>Krasickiego 6, 83-020 Cedry Wielkie, Poland</formatted_address> <address_component> <long_name>6&l

我在解析节点树时遇到了一个问题。下面是我正在使用的XML文档示例:

<result>
    <type>street_address</type>
    <formatted_address>Krasickiego 6, 83-020 Cedry Wielkie, Poland</formatted_address>
    <address_component>
        <long_name>6</long_name>
        <short_name>6</short_name>
        <type>street_number</type>
    </address_component>
    <address_component>
        <long_name>Krasickiego</long_name>
        <short_name>Krasickiego</short_name>
        <type>route</type>
    </address_component>
    <address_component>
        <long_name>Cedry Wielkie</long_name>
        <short_name>Cedry Wielkie</short_name>
        <type>locality</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>Gmina Cedry Wielkie</long_name>
        <short_name>Gmina Cedry Wielkie</short_name>
        <type>administrative_area_level_3</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>Gdańsk County</long_name>
        <short_name>Gdańsk County</short_name>
        <type>administrative_area_level_2</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>Pomeranian Voivodeship</long_name>
        <short_name>Pomeranian Voivodeship</short_name>
        <type>administrative_area_level_1</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>Poland</long_name>
        <short_name>PL</short_name>
        <type>country</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>83-020</long_name>
        <short_name>83-020</short_name>
        <type>postal_code</type>
    </address_component>
</result>

我想从这棵树上得到国家代码PL和邮政编码83-020。我相信我能够在树中搜索包含文本country和postal_code的节点,并获取具有正确名称的同级节点。例如,搜索country并获取名为short_name的同级节点,但我不确定如何操作。我能得到一些朝正确方向的帮助吗?我正在使用C语言并使用XPath。

此XPath查询应返回短的\u name节点,该节点的同级节点名为type,值为postal\u code:

XmlNode node = doc.SelectSingleNode("result/address_component[type='postal_code']/short_name")
执行相同操作以查找国家/地区代码:

XmlNode node = doc.SelectSingleNode("result/address_component[type='country']/short_name")

请添加一些代码,显示您尝试了什么以及查询失败的确切位置。这将帮助这里的专家更好地回答您的问题。有关指南,请参见。谢谢