开发者

Using Linq to XML for XPath Style Document Searching

开发者 https://www.devze.com 2023-04-09 16:28 出处:网络
Let\'s say I\'ve got a web service response which looks like this: <soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xm

Let's say I've got a web service response which looks like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <PMWSGetReportResponse xmlns="http://localhost/PCMilerWS/">
      <PMWSGetReportResult>
        <ReportResponse>
          <Version>25.02</Version>
          <Report>
            <RptType>m</RptType>
            <TripID>011535</TripID>
            <StopNum>1</StopNum>
            <MilesReport>
              <StpLineType>
                <SRStop>
                  <Region>NA</Region>
                  <Address1 />
                  <City>Mill Valley</City>
                  <State>CA</State>
                  <Zip>94941</Zip>
                  <Juris>Marin</Juris>
                  <Lat>37894200</Lat>
                  <Long>-122493488</Long>
                </SRStop>
                <LMiles>0.0</LMiles>
                <TMiles>0.0</TMiles>
                <LCostMile>0.00</LCostMile>
                <TCostMile>0.00</TCostMile>
                <LHours>0:00</L开发者_开发问答Hours>
                <THours>0:00</THours>
                <LTolls>0.00</LTolls>
                <TTolls>0.00</TTolls>
                <LEstghg>0.0</LEstghg>
                <TEstghg>0.0</TEstghg>
              </StpLineType>
              <StpLineType>
                <SRStop>
                  <Region>NA</Region>
                  <Address1 />
                  <City>San Francisco</City>
                  <State>CA</State>
                  <Zip>94109</Zip>
                  <Juris>San Francisco</Juris>
                  <Lat>37790599</Lat>
                  <Long>-122418809</Long>
                  <StopWarning>False</StopWarning>
                </SRStop>
                <LMiles>13.2</LMiles>
                <TMiles>13.2</TMiles>
                <LCostMile>34.33</LCostMile>
                <TCostMile>34.33</TCostMile>
                <LHours>0:17</LHours>
                <THours>0:17</THours>
                <LTolls>12.50</LTolls>
                <TTolls>12.50</TTolls>
                <LEstghg>48.7</LEstghg>
                <TEstghg>48.7</TEstghg>
              </StpLineType>
            </MilesReport>
          </Report>
        </ReportResponse>
      </PMWSGetReportResult>
    </PMWSGetReportResponse>
  </soap:Body>
</soap:Envelope>

I've been trying to use Linq to XML to retrieve, say, the list of all StpLineType nodes. I've tried doing so with something like:

XDocument xdoc = XDocument.Load(new StringReader(soapResponse));
var results = xdoc.Descendants("StpLineType");

But nothing is returned. I can see that the document is loaded into xdoc, but I can't seem to correctly query it. FWIW, I've also tried:

XDocument xdoc = XDocument.Load(new StringReader(soapResponse));
XNamespace ns = "soap";
var results = xdoc.Descendants(ns + "StpLineType");

Just in case the namespace was causing the problem - no luck.

What am I missing?


You're ignoring the two XML namespaces on this document!

  • The root level and the <body> tag are in the soap namespace
  • the rest of the document is in the "default" namespace (without prefix) defined on the <PMWSGetReportResponse> element

If you take that into account - things begin to work just fine!

XDocument xdoc = XDocument.Load(new StringReader(soapResponse));

XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns = "http://localhost/PCMilerWS/";

var results = xdoc.Root.Element(soap + "Body").Descendants(ns + "StpLineType");

But the main question really is: why on earth are you manually parsing a SOAP response?? .....


You should register the "soap" namespace using the XmlNamespaceManager.

Check out this post:

XDocument containing namespaces


Try

XDocument xdoc = XDocument.Load(new StringReader(soapResponse));
XNamespace ns = "http://localhost/PCMilerWS/";
var results = xdoc.Descendants(ns + "StpLineType");
0

精彩评论

暂无评论...
验证码 换一张
取 消