简体   繁体   中英

read child nodes 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;

                    }
            }

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

Also is there any better solution for this?

Update1

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

If you only need the Location element then you can do it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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