2012-01-24 57 views
0

我有如下所示的复杂soap XML。在windows phone 7中解析复杂的soap XML

<?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
     <soap:Header> 
     <MessageHeader> 
      <From>   
      <Type>string</Type> 
      </d3p1:From> 
      <d3p1:To>   
      <Role>string</Role> 
      </d3p1:To>  
     </MessageHeader> 
     <Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/sxvt">  
      <StrongToken>string</StrongToken> 
     </Security> 
     </soap:Header> 
     <soap:Body> 
     <FunctionResponse xmlns="http://www.yyy.com/webservices"> 
      <FunctionRS TimeStamp="dateTime"> 
      <Message>string<Message> 
      <Success> 
       <SuccessMessage>string</SuccessMessage> 
      </Success> 
      <Warnings> 
       <Warning Type="string" Text="string" /> 
       <Warning Type="string" Text="string" /> 
      </Warnings> 
      <Errors> 
       <Error Type="string" Text="string" /> 
       <Error Type="string" Text="string" /> 
      </Errors> 
      <Items> 
       <Item SequenceNo="Int" "> 
       <SamplePrice> 
        <Prices>   
         <Price> 
          <ToatlPrice> 
           <ItemNo>Int </ItemNo> 
           <ItemPrice>Int </ItemPrice> 
          </ToatlPrice> 
         </Price> 
        </Prices> 
       </SamplePrice > 
       </Item> 
      <Item SequenceNo="Int" "> 
       <SamplePrice> 
        <Prices>   
         <Price> 
          <ToatlPrice> 
           <ItemNo>Int </ItemNo> 
           <ItemPrice>Int </ItemPrice> 
          </ToatlPrice> 
         </Price> 
        </Prices> 
       </SamplePrice > 
       </Item> 
      </Items>   
      <Info> 
       <CurrencyCode> 
        <string>string</string> 
        <string>string</string> 
       </CurrencyCode> 
      </Infor> 
      </FunctionRS> 
     </FunctionResponse> 
     </soap:Body> 
    </soap:Envelope> 

这里我想要FunctionRS标记的结果。我创建了FunctionRS标签的类。 我已经创建了FunctionRS类。使用

var result = resultNewDataSet.Descendants("FunctionRS").Select(t => new FunctionRS 
       { 
        Message = t.Descendants("Message").First().Value, 
        //Success = t.Descendants("Success").First().Value 
       }); 

上面的代码中,我能够获得信息的标签,但我不能够得到数组列表(如成功,警告,邮件等)和类(如信息)。 我如何使用LINQ to XML序列化上述xml。

在此先感谢。

+0

你应该阅读上涨约XML命名空间。 –

回答

0

您正在查找的元素位于http://www.yyy.com/webservices命名空间,但是在您的查询中,您没有使用命名空间。我不确定FunctionRS或Message如何在空名称空间中查找它们。请尝试以下操作:

var resultNewDataSet = XDocument.Parse(
@"<?xml version=""1.0"" encoding=""utf-8""?> 
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:d3p1=""unknownnamespace""> 
    <soap:Header> 
    <MessageHeader> 
     <d3p1:From>   
     <Type>string</Type> 
     </d3p1:From> 
     <d3p1:To>   
     <Role>string</Role> 
     </d3p1:To>  
    </MessageHeader> 
    <Security xmlns=""http://schemas.xmlsoap.org/ws/2002/12/sxvt"">  
     <StrongToken>string</StrongToken> 
    </Security> 
    </soap:Header> 
    <soap:Body> 
    <FunctionResponse xmlns=""http://www.yyy.com/webservices""> 
     <FunctionRS TimeStamp=""dateTime""> 
     <Message>string</Message> 
     <Success> 
      <SuccessMessage>string</SuccessMessage> 
     </Success> 
     <Warnings> 
      <Warning Type=""string"" Text=""string"" /> 
      <Warning Type=""string"" Text=""string"" /> 
     </Warnings> 
     <Errors> 
      <Error Type=""string"" Text=""string"" /> 
      <Error Type=""string"" Text=""string"" /> 
     </Errors> 
     <Items> 
      <Item SequenceNo=""Int""> 
      <SamplePrice> 
       <Prices>   
        <Price> 
         <ToatlPrice> 
          <ItemNo>Int </ItemNo> 
          <ItemPrice>Int </ItemPrice> 
         </ToatlPrice> 
        </Price> 
       </Prices> 
      </SamplePrice > 
      </Item> 
     <Item SequenceNo=""Int""> 
      <SamplePrice> 
       <Prices>   
        <Price> 
         <ToatlPrice> 
          <ItemNo>Int </ItemNo> 
          <ItemPrice>Int </ItemPrice> 
         </ToatlPrice> 
        </Price> 
       </Prices> 
      </SamplePrice > 
      </Item> 
     </Items>   
     <Info> 
      <CurrencyCode> 
       <string>string</string> 
       <string>string</string> 
      </CurrencyCode> 
     </Info> 
     </FunctionRS> 
    </FunctionResponse> 
    </soap:Body> 
</soap:Envelope>"); 

XNamespace webServicesNs = "http://www.yyy.com/webservices"; 

var result = resultNewDataSet 
    .Descendants(webServicesNs + "FunctionRS") 
    .Select(t => new 
    { 
     Message = (string)t.Descendants(webServicesNs + "Message").First(), 
     Success = (string)t.Descendants(webServicesNs + "Success").First(), 
     Warnings = t 
      .Element(webServicesNs + "Warnings") 
      .Elements(webServicesNs + "Warning") 
      .Select(w => new 
      { 
       @Type = (string)w.Attribute("Type"), 
       @Text = (string)w.Attribute("Text") 
      }) 
}); 

foreach (var r in result) 
{ 
    Console.WriteLine(r); 
    foreach (var w in r.Warnings) 
    { 
     Console.WriteLine(w); 
    } 
} 

(我包含在XML,因为您提供的一个被打破,我不得不解决它,使它可以加载到的XDocument)。

这是我得到的结果是:

{ Message = string, Success = string, Warnings = System.Linq.Enumerable+WhereSel 
ectEnumerableIterator`2[System.Xml.Linq.XElement,<>f__AnonymousType0`2[System.St 
ring,System.String]] } 
{ Type = string, Text = string } 
{ Type = string, Text = string }