3

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.

2 Answers 2

4

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;
                    }

                }
            }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Is it possible to pick and choose which element I want to keep? For example, if I don't care about <Urbanization> and just want to store Address2, City, State in an "AddressInfo" object.
I used this to get the fields that I needed: var AddressInfoList = doc.Descendants("GetAddressResponse").Descendants("DefaultAddress").Select(Address => new{ Address = Address.Element("Address2").Value, City = Address.Element("City").Value, State = Address.Element("State").Value, Zip5 = Address.Element("Zip5").Value, Zip4 = Address.Element("Zip4").Value, DPC = Address.Element("DeliveryPoint").Value, }).ToList(); foreach (var AddressInfo in AddressInfoList) {
You can use same method SelectSingleNode, pleae check another example..in answer
0

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

                }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.