2011-12-31 1 views
1

Я пишу приложение Silverlight, которое вызывает веб-службу SharePoint. Я получаю ответ, но я не понял правильный синтаксис LINQ, чтобы прочитать значение элемента «ErrorCode». Любая помощь приветствуется.Как использовать LINQ для чтения ответа службы SharePoint?

Вот ответ SharePoint:

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

Я закодировав ниже ответ, так что легче для вас, чтобы проверить:

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.???????????????????? 

Я попытался следующие:

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

спасибо.

ответ

1

Вам необходимо включить пространство имен, например:

var errCode = response 
    .Element("{http://schemas.microsoft.com/sharepoint/soap/}Result") 
    .Element("{http://schemas.microsoft.com/sharepoint/soap/}ErrorCode") 
    .Value; 
+0

Спасибо так много DasBlinkEnlight, он прекрасно работает в настоящее время. –