读取子节点xml [英] read child nodes xml

查看:36
本文介绍了读取子节点xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在尝试阅读的 xml.

Here is my xml which i am trying to read.

<VacancyList xmlns="urn:abc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" generated="2016-04-20T11:42:47" xsi:schemaLocation="http://www.abc.in/dtd/vacancy-list.xsd">
  <Vacancy id="1619993" date_start="2016-04-15" date_end="2016-04-22" reference_number="">
     <Versions>
       <Version language="nb">
         <Title>Marketing Specialist</Title>
         <TitleHeading/>
         <Location>CXCXC</Location>
         <Engagement/>
         <DailyHours/>
         <Region>
            <County id="11">sds</County>
            <County id="1">zxzx</County>
            </Country>
         </Region>
         <Categories>
           <Item type="position-type" id="3909">sER</Item>
           <Item type="duration" id="contract">ss</Item>
           <Item type="extent" id="fulltime">sd</Item>
           <Item type="operating-time" id="day">s</Item>
         </Categories>
       </Version>
     </Versions>

 </Vacancy>

</VacancyList>

我想读取节点位置所以写在下面的代码

I want to read node location so wrote below code

XmlDocument xd = new XmlDocument();
        xd.Load("https://abc.in/list.xml");
        XmlNamespaceManager ns = new XmlNamespaceManager(xd.NameTable);
        ns.AddNamespace("msbld", "urn:abc");

        XmlNodeList nodelist = xd.SelectNodes("//msbld:VacancyList", ns);



        if (nodelist != null)
            foreach (XmlNode node in nodelist)
            {
                XmlNode nodelist1 = node.SelectSingleNode("Vacancy");
                if (nodelist1 != null)
                    foreach (XmlNode node1 in nodelist1)
                    {
                       var k = node1.Attributes.GetNamedItem("Location").Value;

                    }
            }

但是我在变量node1"中没有得到任何东西.如何解决这个问题?

But i dont get anything in variable "node1". How to fix this?

还有没有更好的解决方案?

Also is there any better solution for this?

更新 1

我修改了代码,但我只得到节点标题.不能让其他人进入版本节点,比如位置.

i modified code but i only get node Title. cant get others inside Version node like Location.

if (nodelist != null)
            foreach (XmlNode node in nodelist)
            {
                XmlNode nodelist1 = node.SelectSingleNode("//msbld:Vacancy/msbld:Versions",ns);
                if (nodelist1 != null) { 
                    XmlNode nodelist2 = nodelist1.SelectSingleNode("//msbld:Version", ns);
                    foreach (XmlNode node3Node in nodelist2)
                    {
                        var k = node3Node.Attributes.GetNamedItem("Location").Value;
                    }


                }
            }

推荐答案

xmlns="urn:abc" 是一个默认命名空间.请注意,没有前缀的后代元素隐式继承祖先的默认命名空间.您还需要使用引用默认命名空间 URI 的相同前缀来访问 VacancyLocation :

xmlns="urn:abc" is a default namespace. Notice that descendant elements without prefix inherits ancestor's default namespace implicitly. You need to use the same prefix that references default namespace URI for acessing Vacancy and Location as well :

XmlNode nodelist1 = node.SelectSingleNode("msbld:Vacancy", ns);

<小时>

您更新后的代码引入了一个完全不同的问题;路径表达式开头的 / 将始终引用文档元素,除非您在 / 之前使用 . 明确地将上下文设置为当前活动上下文,例如:


Your updated code introduces an entirely different problem; / at the beginning of a path expression will always reference document element, unless you explicitly set the context to current active context by using . before /, for example :

XmlNode nodelist1 = node.SelectSingleNode(".//msbld:Vacancy/msbld:Versions",ns);

这篇关于读取子节点xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆