Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the 'exception.Detail.LastChild.OuterXML' from a SOAP exception( System.Web.Services.Protocols.SoapException) returned to a program in .NET.

I can code to parse this string in a long hand kind of way to retrieve the values i need. But i'd rather do it using methods specifically designed to work with XML structures....even though that is obviously new to me. Ive attempted to retrieve the values i'm after in a number of different ways. But i'm having no luck and feel like i'm getting farther from the solution.

So, my question: What would be the suggested method for retrieving the values for:
Severity, Code and Description in the attached xml?
Thank you in advance for any insights:)

HTML
<detail><err:Errors xmlns:err="http://www.zzzz.com/XMLSchema/XOLTWS/Error/v1.1"><err:ErrorDetail><err:Severity>Hard</err:Severity><err:PrimaryErrorCode><err:Code>9120203</err:Code><err:Description>Missing or invalid Address.</err:Description></err:PrimaryErrorCode></err:ErrorDetail></err:Errors></detail>
Posted

1 solution

Try Like Below
C# code:
C#
string xml =@"<detail><err:Errors xmlns:err=""http://www.zzzz.com/XMLSchema/XOLTWS/Error/v1.1""><err:ErrorDetail><err:Severity>Hard</err:Severity><err:PrimaryErrorCode><err:Code>9120203</err:Code><err:Description>Missing or invalid Address.</err:Description></err:PrimaryErrorCode></err:ErrorDetail></err:Errors></detail>";
var element =XElement.Parse(xml);
string Severity =element.Descendants().FirstOrDefault(e => e.Name.LocalName =="Severity" ).Value;
string Code =element.Descendants().FirstOrDefault(e => e.Name.LocalName =="Code" ).Value;
string Description =element.Descendants().FirstOrDefault(e => e.Name.LocalName =="Description" ).Value;


VB code
VB
Dim xml As String = "<detail><err:Errors xmlns:err=""http://www.zzzz.com/XMLSchema/XOLTWS/Error/v1.1""><err:ErrorDetail><err:Severity>Hard</err:Severity><err:PrimaryErrorCode><err:Code>9120203</err:Code><err:Description>Missing or invalid Address.</err:Description></err:PrimaryErrorCode></err:ErrorDetail></err:Errors></detail>"
Dim element = XElement.Parse(xml)
Dim Severity As String = element.Descendants().FirstOrDefault(Function(e) e.Name.LocalName = "Severity").Value
Dim Code As String = element.Descendants().FirstOrDefault(Function(e) e.Name.LocalName = "Code").Value
Dim Description As String = element.Descendants().FirstOrDefault(Function(e) e.Name.LocalName = "Description").Value
 
Share this answer
 
v3
Comments
rvanb 20-Jun-14 12:45pm    
Thank you DamithSL!! I was way off mark with what i'd attempted. Always something new to learn:)
DamithSL 20-Jun-14 12:54pm    
You are Welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900