Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My C# code is not working as I expect. It's only displaying Fax #
I have xml with phone numbers which I want to loop through in C# and then display the phone numbers in the following order.
Home #, Cell #, Work #, Fax #
This is what I need to do.
1. If the xml document has multiple phone numbers with Phone/@Current="true", select the current record where the type = Home (Party\Phone\Type="HOME")
2. If there is no home phone type, then select current record where type = CELL (Party\Phone\Type="CELL")
3. If there is no cell phone type, then select current record where type = WORK (Party\Phone\Type="WORK")
4. If there is no work phone type, then select current record where type = FAX (Party\Phone\Type="FAX")

xml document
XML
<?xml version="1.0" encoding="UTF-8"?>
<Integration> 
	<Party ID="1">
		<Phone>
			<Type Word="WORK">Work</Type>
			<Number>218-222-2222</Number>
		</Phone>
		<Phone Current="true"> 
			<Type Word="FAX">Fax</Type>
			<Number>218-777-7777</Number>
		</Phone>
		<Phone>
			<Type Word="HOME">Home</Type>
			<Number>218-333-3333</Number>
		</Phone>
		<Phone>
			<Type Word="CELL">Cell</Type>
			<Number>218-555-5555</Number>
		</Phone>
		<Phone Current="true">
			<Type Word="CELL">Cell</Type>
			<Number>218-666-6666</Number>
		</Phone>
		<Phone Current="true">
			<Type Word="HOME">Home</Type>
			<Number>218-300-0011</Number>
		</Phone>
		<Phone Current="true">
			<Type Word="WORK">Work</Type>
			<Number>218-111-0000</Number>
		</Phone>
		<Phone> 
			<Type Word="FAX">Fax</Type>
			<Number>218-000-9999</Number>
		</Phone>
</Integration>


Required output
XML
<?xml version="1.0" encoding="UTF-8"?>
<Integration> 
	<Party ID="1">
		<Phone Current="true">
			<Type Word="HOME">Home</Type>
			<Number>218-300-0011</Number>
		</Phone>
		<Phone Current="true">
			<Type Word="CELL">Cell</Type>
			<Number>218-666-6666</Number>
		</Phone>
		<Phone Current="true">
			<Type Word="WORK">Work</Type>
			<Number>218-111-0000</Number>
		</Phone>
		<Phone Current="true"> 
			<Type Word="FAX">Fax</Type>
			<Number>218-777-7777</Number>
		</Phone>
</Integration>



C# code
VB
Boolean blnPhoneFound = false;
            XmlNode objxmlPhoneNode = objxmlBasePartyNode.SelectSingleNode("Phone[@Current='true']");
            if (objxmlPhoneNode != null)
            {
                objCaseParty.Phone = new ConservatorService.Phone();
                if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Home")
                {
                    objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Home;
                    blnPhoneFound = true;
                }
                else //Cell
                {
                    if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Cell")
                    {
                        objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Cell;
                        blnPhoneFound = true;
                    }
                    else //Work
                    {
                        if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Work")
                        {
                            objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Work;
                            blnPhoneFound = true;
                        }

                        else //Fax
                        {
                            if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Fax")
                            {
                                objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Fax;
                                blnPhoneFound = true;
                            }

                        } //Fax
                    } //Work
                } // Cell
                if (blnPhoneFound)
                {
                    if (objxmlPhoneNode.SelectSingleNode("Extension") != null)
                    {
                        string strExtension = objxmlPhoneNode.SelectSingleNode("Extension").InnerText;

                        objCaseParty.Phone.Extention = strExtension;
                    }
                    if (objxmlPhoneNode.SelectSingleNode("Number") != null)
                    {
                        string strNumber = objxmlPhoneNode.SelectSingleNode("Number").InnerText;
                        objCaseParty.Phone.Number = strNumber;
                    }
                }

            } //Phone Current 
           
        }

    }
}


What I have tried:

C#
Boolean blnPhoneFound = false;
            XmlNode objxmlPhoneNode = objxmlBasePartyNode.SelectSingleNode("Phone[@Current='true']");
            if (objxmlPhoneNode != null)
            {
                objCaseParty.Phone = new ConservatorService.Phone();
                if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Home")
                {
                    objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Home;
                    blnPhoneFound = true;
                }
                else //Cell
                {
                    if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Cell")
                    {
                        objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Cell;
                        blnPhoneFound = true;
                    }
                    else //Work
                    {
                        if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Work")
                        {
                            objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Work;
                            blnPhoneFound = true;
                        }

                        else //Fax
                        {
                            if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Fax")
                            {
                                objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Fax;
                                blnPhoneFound = true;
                            }

                        } //Fax
                    } //Work
                } // Cell
                if (blnPhoneFound)
                {
                    if (objxmlPhoneNode.SelectSingleNode("Extension") != null)
                    {
                        string strExtension = objxmlPhoneNode.SelectSingleNode("Extension").InnerText;

                        objCaseParty.Phone.Extention = strExtension;
                    }
                    if (objxmlPhoneNode.SelectSingleNode("Number") != null)
                    {
                        string strNumber = objxmlPhoneNode.SelectSingleNode("Number").InnerText;
                        objCaseParty.Phone.Number = strNumber;
                    }
                }

            } //Phone Current 
           
        }

    }
}
Posted
Updated 9-Dec-16 6:36am
v2
Comments
ZurdoDev 9-Dec-16 11:10am    
This is a very, very easy thing for you to fix. Put a breakpoint in your code and then step through each line and observe what is happening. You'll very quickly find the problem.

We can't run your code so you need to do this.
Member 11403304 9-Dec-16 14:00pm    
When I debug my code it seems the code is returning the Fax number because that is what it finds first in my xml document. However even though this fax number has current in it, I want to display the Home phone first before the fax number. I am not sure how to do this. The order in xml document is not how I want the numbers displayed.

1 solution

I would de-serialize the xml data into an appropriate model, and then use LINQ to find the crap I want in the model instead of dickin' around with parsing the xml on the fly. But that's just me.
 
Share this answer
 
Comments
Member 11403304 13-Dec-16 7:46am    
The order in which the phone elements appear in the xml is not important. What is important is that regardless of where phone type Home element is located in xml, that is the number I want to display unless it does not exist in which case I will display Cell and if that does not exist then display work and if that does not exist display Fax as the last option.
My code is only displaying the first element of phone type which has phone/@Current='true' which in my xml is Fax. I do not want to display Fax number because there is Home number
How do I logically, select all the nodes with Current equal to true, and sort these items by Type (selecting the first of these is all I need). It sounds simple but I am not sure how or what to change in my code. That way I want to only display phone type Home number if it is found even if there are other phone types found. If phone type Home is not found, then I want to display phone type Cell number, if not found I want to display phone type Work number and if all above are not found, I want to display phone type Fax number.
The output I expect should be
<phone>
<number>218-300-0011>
<type>Home

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900