0

This is my code to retrieve an element from XML:

private async Task<XElement> PostAsXmlRequestAsync(XElement parameter, CancellationToken responseToken)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(Constants.url);
        HttpResponseMessage responseMessage = await client.PostAsXmlAsync(Constants.uri, parameter, responseToken);

        string responseContent = await responseMessage.Content.ReadAsStringAsync();
        XElement responseXml = XElement.Parse(responseContent);

        **bool hasElement = responseXml.Elements("Warnings").Any();
        Console.WriteLine(hasElement);**

        return responseXml;
    }
}

responseXml has a Warnings element, but hasElement is always false.

This is sample XML from the response:

<OTA_VehAvailRateRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opentravel.org/OTA/2003/05" Target="Qual" Version="2.007">
    <Success /> 
    <Warnings>
        <Warning Type="1" ShortText="FOR RENTERS UNDER 25 YRS OF AGE SEE KEYWORD AGE" RecordID="201" /> 
    </Warnings>
    <VehAvailRSCore>
        <VehRentalCore PickUpDateTime="2014-05-01T10:00:00-05:00" ReturnDateTime="2014-05-03T10:00:00-05:00">
            <PickUpLocation ExtendedLocationCode="LAXT15" LocationCode="LAX" /> 
            <ReturnLocation ExtendedLocationCode="LAXT15" LocationCode="LAX" /> 
        </VehRentalCore>
    </VehAvailRSCore>
</OTA_VehAvailRateRS>
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
user3444535
  • 233
  • 1
  • 2
  • 12

1 Answers1

0

Likely node you are looking for have some namespace instead of default empty namespace (i.e. XML have something like xmlns="http://some-namespace").

You should use version that takes name with namespace to select element. XContainer.Elements article shows both cases as samples:

XNamespace aw = "http://www.adventure-works.com";
var elements = xmlTree.Elements(aw + "Type2");
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thank you, but It's not working, always elements value is null – user3444535 Mar 21 '14 at 01:54
  • @user3444535 so you've used `"http://www.opentravel.org/OTA/2003/05"` as `XNamespace` and code still did not work? – Alexei Levenkov Mar 21 '14 at 02:32
  • @user3444535 - please edit your post with "also tried..." - code in comment is hard to read. I.e. it is unclear if you did `XNamespace a = "http://www.opentravel.org/OTA/2003/05";` or incorrectly `XNamespace a = "www.opentravel.org/OTA/2003/05";` – Alexei Levenkov Mar 21 '14 at 02:57
  • sorry, `XNamespace xmlns = "http://www.opentravel.org/OTA/2003/05";` `var elements = responseXml.Elements(xmlns + "OTA_VehAvailRateRS");` `Console.WriteLine(elements);` it's correct coeding. it's not work. elements is null... – user3444535 Mar 21 '14 at 04:36
  • @user3444535 `Elements` never return `null` to my knowledge... Anyway if you read the MSDN link you'd notice "Returns a filtered collection of the **child elements** of **this** element" - you asking to child element that have the same name as "this element" - indeed you get *empty collection* as result. – Alexei Levenkov Mar 21 '14 at 04:46
  • `XNamespace xmlns = "http://www.opentravel.org/OTA/2003/05";` `XDocument myDocument = XDocument.Parse(responseContent, LoadOptions.None);` `bool hasElements = myDocument.Elements(xmlns + "OTA_VehAvailRateRS").Elements(xmlns + "Warnings").Any();` this code is working now. it's XDocument Type. I don't know why XElement not working. Thank you very much!!!. – user3444535 Mar 21 '14 at 05:02
  • @user3444535 because document is *parent* of first element. – Alexei Levenkov Mar 21 '14 at 05:48