简体   繁体   English

读取子节点xml

[英]read child nodes xml

Here is my xml which i am trying to read. 这是我尝试读取的XML。

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

                    }
            }

But i dont get anything in variable "node1". 但是我没有在变量“ node1”中得到任何东西。 How to fix this? 如何解决这个问题?

Also is there any better solution for this? 还有没有更好的解决方案呢?

Update1 更新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" is a default namespace . xmlns="urn:abc"默认的名称空间 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 : 您还需要使用引用默认名称空间URI的相同前缀来访问VacancyLocation

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

If you only need the Location element then you can do it like this: 如果只需要Location元素,则可以这样:

var doc = XElement.Load("path/to/file");
var location = doc.Descendants
    .FirstOrDefault(e => e.Name.LocalName == "Location"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM