英文:
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:
<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.
答案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("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
答案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("Errors") select nm);
to this
var xmlerrors = XElement.Parse(response.Content).Elements().Where(e => e.Name.LocalName.ToLower() == "errors").ToArray();
Hope this helps someone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论