0

以下是我从 Web 服务生成的响应。我想做这样的事情,我只想要PresentationElements这个响应中的节点。任何帮助我怎样才能实现这个查询?

<?xml version="1.0"?>
<GetContentResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ExtensionData />
  <GetContentResult>
    <ExtensionData />
    <Code>0</Code>
    <Value>Success</Value>
  </GetContentResult>
  <PresentationElements>
    <PresentationElement>
      <ExtensionData />
      <ContentReference>Product View Pack</ContentReference>
      <ID>SHOPPING_ELEMENT:10400044</ID>
      <Name>View Pack PE</Name>
      <PresentationContents>
        <PresentationContent>
          <ExtensionData />
          <Content>View Pack</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Name</Name>
        </PresentationContent>
        <PresentationContent>
          <ExtensionData />
          <Content>Have more control of your home's security and lighting with View Pack from XFINITY Home.</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Description</Name>
        </PresentationContent>
        <PresentationContent>
          <ExtensionData />
          <Content>/images/shopping/devices/xh/view-pack-2.jpg</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Image</Name>
        </PresentationContent>
        <PresentationContent>
          <ExtensionData />
          <Content>The View Pack includes:
2 Lighting / Appliance Controllers
2 Indoor / Outdoor Cameras</Content>
          <ContentType>TEXT</ContentType>
          <Language>ENGLISH</Language>
          <Medium>COMPUTER_BROWSER</Medium>
          <Name>Feature1</Name>
        </PresentationContent>
      </PresentationContents>
    </PresentationElement>
  </PresentationElements>
</GetContentResponse>
4

3 回答 3

5

您可以使用 XPath 扩展

var xdoc = XDocument.Parse(response);
XElement presentations = xdoc.XPathSelectElement("//PresentationElements");
于 2013-04-23T09:30:31.550 回答
3

您可以使用System.Xml.Linq.XDocument

//Initialize the XDocument
XDocument doc = XDocument.Parse(yourString);

//your query
var desiredNodes = doc.Descendants("PresentationElements");
于 2013-04-23T09:15:19.613 回答
1

很简单,你试过吗:

XDocument xml = XDocument.Load("... xml");
var nodes = (from n in xml.Descendants("PresentationElements")
                        select n).ToList();

您还可以使用以下方式将每个单独的节点投影到匿名类型:

select new 
{
  ContentReference = (string)n.Element("ContentReference").Value,
  .... etc
}
于 2013-04-23T09:25:44.430 回答