在Windows Phone 7中解析复杂的soap XML

发布于 2024-12-28 18:00:25 字数 3407 浏览 0 评论 0原文

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

提前致谢。

I have complex soap XML like below.

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

here i want the results of FunctionRS tag. I have created the class for the FunctionRS tag.
I have created FunctionRS class.

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

using the above code i am able to get Message tag, but i am not able get the array lists (like Success, warnings,Items,etc) and class (like Info).
How can i Serialize the above xml using LINQ to XML.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2025-01-04 18:00:25

您要查找的元素位于 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 }

Elements you are looking for are in http://www.yyy.com/webservices namespace however in your query you are not using namespaces. I am not sure how FunctionRS or Message could be found as you are looking for them in the empty namespace. Try the following:

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);
    }
}

(I included the Xml since the one you provided was broken and I had to fix it to make it possible to load to XDocument).

Here is the result I got:

{ 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 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文