【问题标题】:Parsing complex soap XML in windows phone 7在 Windows Phone 7 中解析复杂的肥皂 XML
【发布时间】:2012-01-24 07:47:37
【问题描述】:

我有如下复杂的肥皂 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。

提前致谢。

【问题讨论】:

  • 您应该阅读有关 XML 命名空间的内容。

标签: windows-phone-7 linq-to-xml


【解决方案1】:

您要查找的元素位于 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,因为您提供的 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 }

【讨论】:

    猜你喜欢
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多