0

I'm trying to get the list of errors into the error tag from this XML that I received by RestRequest but my variable xmlerrors (var xmlerrors) is always null:

<PositionOpeningResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ns.hr-xml.org/2006-02-28">
  <PositionRecordInfo>
    <Ids />
  </PositionRecordInfo>
  <ProcessDate>2023-03-09T05:26:13</ProcessDate>
  <ProcessFeedBack>
    <InternetEmailAddress>info@mail.com</InternetEmailAddress>
  </ProcessFeedBack>
  <Warnings />
  <Errors>
    <Error>The element 'PositionDetail' in namespace 'http://ns.hr-xml.org/2006-02-28' has invalid child element 'PhysicalLocation' in namespace 'http://ns.hr-xml.org/2006-02-28'. List of possible elements expected: 'CompanyScale, IndustryCode' in namespace 'http://ns.hr-xml.org/2006-02-28'.</Error>
    <Error>The 'relationship' attribute is not declared.</Error>
  </Errors>
</PositionOpeningResult>

Here my function to get the error list and concat them to display then in a HTML view :

if (response.Content.Contains("Errors"))
{

    var xmlerrors = (from nm in xelement.Elements("Errors") select nm);

    foreach (XElement error in xmlerrors)
    {
        foreach (XElement suberror in error.Elements("Error"))
        {
            errors += suberror.ToString().Replace("<Error>", "<p>").Replace("</Error>", "</p>") + "\r\n";
        }
    }
    ProviderResponse.errors = errors;
}

What am I doing wrong ?

Thanks a lot for your help and feedback.

Mathlan89
  • 23
  • 6
  • There's a default namespace `xmlns="http://ns.hr-xml.org/2006-02-28"`. The elements are all in that namespace. See [LINQ Xelement Returns null when child node exist](https://stackoverflow.com/a/27248207/3744182) and [Use Linq to Xml with Xml namespaces](https://stackoverflow.com/q/2340411), – dbc Mar 09 '23 at 16:53
  • Oh yes, my Bad didn't notice this errors in namespace. Thanks for tour help. :) – Mathlan89 Mar 09 '23 at 19:52

2 Answers2

1

This should work.

if (response.Content.Contains("Errors"))
{
    var xdoc = XDocument.Parse(response.Content);
    XNamespace ns = "http://ns.hr-xml.org/2006-02-28";

    var xmlerrors = (from nm in xdoc.Descendants(ns + "Errors") select nm);

    foreach (var error in xmlerrors)
    {
        foreach (var suberror in error.Elements(ns + "Error"))
        {
            errors += suberror.ToString().Replace("<Error>", "<p>").Replace("</Error>", "</p>") + "\r\n";
        }
    }
    ProviderResponse.errors = errors;
}

What you are doing wrong is that you should be using

Elements("{http://ns.hr-xml.org/2006-02-28}Errors")

since the "Errors" element is in that namespace

AVTUNEY
  • 923
  • 6
  • 11
0

Another option would be to suppress namespace checking...to use this change your code from

var xmlerrors = xelement.Elements("Errors") select nm);

to this

var xmlerrors = XElement.Parse(response.Content).Elements().Where(e => e.Name.LocalName.ToLower() == "errors").ToArray();

Hope this helps someone.

Cylian
  • 10,970
  • 4
  • 42
  • 55