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
="1.0"="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
="1.0"="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
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:
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
{
if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Cell")
{
objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Cell;
blnPhoneFound = true;
}
else
{
if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Work")
{
objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Work;
blnPhoneFound = true;
}
else
{
if (objxmlPhoneNode.SelectSingleNode("Type").InnerText == "Fax")
{
objCaseParty.Phone.Type = ConservatorService.PhoneNumberTypes.Fax;
blnPhoneFound = true;
}
}
}
}
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;
}
}
}
}
}
}