简体   繁体   中英

Extract a node from xml response

Below is my response generated from a webservice. I want to do such that I want only PresentationElements node from this response. Any help how can I achieve this query?

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

You can use XPath extensions

var xdoc = XDocument.Parse(response);
XElement presentations = xdoc.XPathSelectElement("//PresentationElements");

You may use the System.Xml.Linq.XDocument :

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

//your query
var desiredNodes = doc.Descendants("PresentationElements");

Pretty easy, have you tried:

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

You could also project each individual node to an anonymous type using something like:

select new 
{
  ContentReference = (string)n.Element("ContentReference").Value,
  .... etc
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM