NoBullMan
NoBullMan

Reputation: 2188

ASP.Net - Parsing XML

I have an XMLDocument object with InnerXml set a response I get from a web service. A sample response is like this:

<GetAddressResponse>
    <AddressStatus>EXACT MATCH</AddressStatus>
    <DefaultAddress>
        <FirmName></FirmName>
        <Address1></Address1>
        <Address2>PO BOX 123</Address2>
        <City>DAYTON</City>
        <State>OH</State>
        <Urbanization></Urbanization>
        <Zip5>45475</Zip5>
        <Zip4>1952</Zip4>
        <CarrierRoute>B017</CarrierRoute>
        <CountyName>MONTGOMERY</CountyName>
        <DeliveryPoint>52</DeliveryPoint>
    </DefaultAddress>
    <AddressRecCount>0</AddressRecCount>
</GetAddressResponse>

I need to be able to first get and check the value of AddressStatus and based on the result, get everything under DefaultAddress. I am using .Net3.5 so I can use Linq if need be. Thank you in advance.

Upvotes: 3

Views: 12690

Answers (2)

Thit Lwin Oo
Thit Lwin Oo

Reputation: 3448

// for single node selection..

if (node != null)
            {
                string addStatus = node.InnerText.Trim();

                if (addStatus.ToUpper() == "EXACT MATCH")
                {
                    XmlNode addNode = doc.SelectSingleNode("GetAddressResponse/DefaultAddress");

                    //only Zip5 is selected if exists
                    XmlNode childNode = addNode.SelectSingleNode("Zip5");

                    if (childNode != null) { string zip = childNode.InnerText; }

                    //select city
                    childNode = addNode.SelectSingleNode("City");
                    if (childNode != null) { string city = childNode.InnerText; }

                    //etc

                }
            }

Upvotes: 0

Thit Lwin Oo
Thit Lwin Oo

Reputation: 3448

You can try this way..

string xml = @"<GetAddressResponse>
                            <AddressStatus>EXACT MATCH</AddressStatus>
                            <DefaultAddress>
                                <FirmName></FirmName>
                                <Address1></Address1>
                                <Address2>PO BOX 123</Address2>
                                <City>DAYTON</City>
                                <State>OH</State>
                                <Urbanization></Urbanization>
                                <Zip5>45475</Zip5>
                                <Zip4>1952</Zip4>
                                <CarrierRoute>B017</CarrierRoute>
                                <CountyName>MONTGOMERY</CountyName>
                                <DeliveryPoint>52</DeliveryPoint>
                            </DefaultAddress>
                            <AddressRecCount>0</AddressRecCount>
                        </GetAddressResponse>
                        ";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlNode node = doc.SelectSingleNode("GetAddressResponse/AddressStatus");

            if (node != null)
            {
                string addStatus = node.InnerText.Trim();

                if (addStatus.ToUpper() == "EXACT MATCH")
                {
                    XmlNode addNode = doc.SelectSingleNode("GetAddressResponse/DefaultAddress");

                    foreach (XmlElement ele in addNode.ChildNodes)
                    {
                        //get each child element value
                        string val = ele.InnerText;
                    }

                }
            }

Upvotes: 4

Related Questions