Search code examples
c#xmlxml-parsingxmlhttprequesthttpresponse

Parsing xml response - Select specific node based on value of another node


I'm new to xml so I'm not sure if I worded the question correctly, but I will do my best to explain.

Basically, I'm trying to parse an xml response in C# such as the one below:

<Premium>
        <TotalPremiumAmount>87</TotalPremiumAmount>
        <StandardPremium>87</StandardPremium>
        <OptionalPremium>0</OptionalPremium>
        <StandardTax>0</StandardTax>
        <OptionalTax>0</OptionalTax>
        <ExtendedTax>0</ExtendedTax>
        <ExtendedPremium>0</ExtendedPremium>
        <PromotionalPremium>0</PromotionalPremium>
        <FeesPremium>0</FeesPremium>
        <FeesTax>0</FeesTax>
        <StandardFeesPremium>0</StandardFeesPremium>
        <OptionalFeesPremium>0</OptionalFeesPremium>
        <Tax>0</Tax>
        <StandardPremiumDistribution>
            <Travelers>
                <Traveler>
                    <TravelerPremium>42</TravelerPremium>
                    <TravelerTax>0</TravelerTax>
                </Traveler>
                <Traveler>
                    <TravelerPremium>45</TravelerPremium>
                    <TravelerTax>0</TravelerTax>
                </Traveler>
            </Travelers>
        </StandardPremiumDistribution>
        <PackagePremiumDistribution>
            <Packages>
                <Package>
                    <PackageID>20265</PackageID>
                    <PackageName />
                    <PackageTypeID>12</PackageTypeID>
                    <Premium>87</Premium>
                    <Fees>0</Fees>
                    <Tax>0</Tax>
                    <Travelers>
                        <Traveler>
                            <TravelerID>0</TravelerID>
                            <Premium>42</Premium>
                            <Tax>0</Tax>
                        </Traveler>
                        <Traveler>
                            <TravelerID>1</TravelerID>
                            <Premium>45</Premium>
                            <Tax>0</Tax>
                        </Traveler>
                    </Travelers>
                </Package>
            </Packages>
        </PackagePremiumDistribution>
    </Premium>

I would like to get the value of the (Traveler) Premium. In the case of only one traveler, I have been using an XMLDocument and the 'SelectSingleNode" function. For example I could do something like:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlResponse);
var premium = xmlDoc.SelectSingleNode("//TravelerPremium").InnerText;

But this wouldn't work when multiple travelers are returned under one plan. For example, I need the premium when TravelerID = 0. How would I go about doing this?

Thanks.


Solution

  • Using XmlDocument:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(xmlResponse);
    var premium = xmlDoc.SelectSingleNode("//Premium[../TravelerID = '0']")
    

    You could also iterate through the nodes if multiple could match on this condition like so:

    foreach(var premium in xmldoc.SelectNodes("//Premium[../TravelerID = '0']")
    {
       // do work on each premium node where TravelerID = 0
    }
    

    I'd encourage you to look into using LINQ to XML - it's generally easier to work with and will be more performant in most cases. You could even still use XPath expressions, but the following would work:

    XDocument xdoc = XDocument.Load(xmlResponse);
    var premium = (string)xdoc.Descendants("Traveler").Where(x => (string)x.Element("TravelerID") == "0").Element("Premium");