I'm writing a Silverlight application that invokes a SharePoint Web Service. I'm getting a response, but I haven't figured out the correct LINQ syntax to read the value of the element "ErrorCode". Any help is greatly appreciated.
Here's the SharePoint response:
<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<Result ID="1,New">
<ErrorCode>0x810200bf</ErrorCode>
<ErrorText>The list item could not be added or updated because duplicate values were found in one or more fields in the list.</ErrorText>
</Result>
</Results>
I'm hard-coding the response below, so it's easier for you to test it:
TextReader sr = new StringReader( @"<?xml version=""1.0"" encoding=""utf-8"" ?>" +
@"<Results xmlns=""http://schemas.microsoft.com/sharepoint/soap/"">" +
@"<Result ID=""1,New"">" +
@"<ErrorCode>0x810200bf</ErrorCode>" +
@"<ErrorText>The list item could not be added or updated because duplicate values were found in one or more fields in the list.</ErrorText>" +
@"</Result>" +
@"</Results>");
XElement response = XElement.Load(sr);
sr.Close();
string errorCode = response.????????????????????
I've tried the following:
// Attempt 1:
string errorCode = response.Elements("Results").Elements("Result").First().Value;
// Attempt 2:
string errorCode = response.Descendants(XName.Get("Result")).First().Value;
// Attempt 3:
string errorCode = response.Descendants("Results").Descendants("Result").First().Value;
// Attempt 4:
string errorCode = (from el in response.Elements("Result")
where el.Attribute("ID").Value == "1,New"
select el).First().Value;
Thank you.
You need to include namespace, like this:
var errCode = response
.Element("{http://schemas.microsoft.com/sharepoint/soap/}Result")
.Element("{http://schemas.microsoft.com/sharepoint/soap/}ErrorCode")
.Value;