简体   繁体   中英

How to parse child elements of XML-object with Linq

I am trying to parse this document. Here is a part of it:

<JobPortalPositionList>
    <CustomerAlias>Frogn</CustomerAlias>
    <CustomerName>Frogn kommune</CustomerName>
    <TransactionStatus>
        <Description>The operation completed successfully.</Description>
        <StatusCode>Success</StatusCode>
    </TransactionStatus>
    <Items>
        <JobPortalPosition>
            <CustomerAlias>Frogn</CustomerAlias>
            <CustomerName>Frogn kommune</CustomerName>
            <TransactionStatus>

The url: http://api.hr-manager.net/JobPortal.svc/frogn/positionlist/xml/
My try with Linq:

string uri="http://api.hr-manager.net/JobPortal.svc/frogn/positionlist/xml/";
XElement xmlJobTree = XElement.Load(uri);

var listOfJobpositions =
    (from jobpositions in xmlJobTree.Elements("JobPortalPositionList")
                                    .Elements("Items")
                                    .Elements("JobPortalPosition")
    select jobpositions).ToList();

I get an empty list. What am I doing wrong?

There are a couple of problems with your code. The document has a namespace which your code doesn't account for.

The root of the document is:

<JobPortalPositionList xmlns="http://schemas.hr-manager.net/jobportal/1.0/"
                       xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

Since the default namespace is http://schemas.hr-manager.net/jobportal/1.0/ , your queries need to use them.

XNamespace ns = "http://schemas.hr-manager.net/jobportal/1.0/";
var listOfJobpositions =
    (from jobpositions in xmlJobTree.Elements(ns + "JobPortalPositionList")
                                    .Elements(ns + "Items")
                                    .Elements(ns + "JobPortalPosition")
    select jobpositions).ToList();

Sidenote, that could be rewritten to simply:

var listOfJobpositions =
    xmlJobTree.Elements(ns + "JobPortalPositionList")
              .Elements(ns + "Items")
              .Elements(ns + "JobPortalPosition")
              .ToList();

Secondly, you parsed the document as an XElement . So the parsed element xmlJobTree will refer to the root node of the document, not the document itself. So the first part of your query is checking to see if JobPortalPositionList is an element of the JobPortalPositionList element. That's a definite no so it yields nothing.

You should always parse a complete XML document as an XDocument so you don't confuse yourself here. As an XDocument , queries will be relative to the document, not the root. And everything else will just work.

var xmlJobTree = XDocument.Load(uri);

you can to do something like:

var documents =
    (from docs in documentRoot.Descendants("document")
     select new
     {
         Id = (string) docs.Attribute("id"),
         Sections = docs.Elements("section")
     }).ToList();

foreach (var doc in documents)
{
    foreach (var section in doc.Sections)
    {
        Console.WriteLine("SectionId: " + section.Attribute("id"));
        foreach (var item in section.Elements("item"))
        {
            Console.WriteLine("ItemId: " + item.Attribute("id"));
        }
    }
}

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