获取在C#中使用XElement的XML错误列表。

huangapple go评论63阅读模式
英文:

Trying to get errors list of XML with XElement in C#

问题

以下是翻译好的部分:

"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." 句子不需要翻译。

英文:

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:

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

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

if (response.Content.Contains(&quot;Errors&quot;))
{

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

    foreach (XElement error in xmlerrors)
    {
        foreach (XElement suberror in error.Elements(&quot;Error&quot;))
        {
            errors += suberror.ToString().Replace(&quot;&lt;Error&gt;&quot;, &quot;&lt;p&gt;&quot;).Replace(&quot;&lt;/Error&gt;&quot;, &quot;&lt;/p&gt;&quot;) + &quot;\r\n&quot;;
        }
    }
    ProviderResponse.errors = errors;
}

What am I doing wrong ?

Thanks a lot for your help and feedback.

答案1

得分: 1

这应该可以运行。

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

你做错的是应该使用 Elements("{http://ns.hr-xml.org/2006-02-28}Errors"),因为 "Errors" 元素在那个命名空间中。

英文:

This should work.

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

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

    foreach (var error in xmlerrors)
    {
        foreach (var suberror in error.Elements(ns + &quot;Error&quot;))
        {
            errors += suberror.ToString().Replace(&quot;&lt;Error&gt;&quot;, &quot;&lt;p&gt;&quot;).Replace(&quot;&lt;/Error&gt;&quot;, &quot;&lt;/p&gt;&quot;) + &quot;\r\n&quot;;
        }
    }
    ProviderResponse.errors = errors;
}

What you are doing wrong is that you should be using

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

since the &quot;Errors&quot; element is in that namespace

答案2

得分: 0

另一种选择是禁止命名空间检查...要使用此更改,请将您的代码从

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

更改为

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

希望这对某人有帮助。

英文:

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

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

to this

var xmlerrors = XElement.Parse(response.Content).Elements().Where(e =&gt; e.Name.LocalName.ToLower() == &quot;errors&quot;).ToArray();

Hope this helps someone.

huangapple
  • 本文由 发表于 2023年3月10日 00:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定