How do I get the home phone type from xml doc?

momokesh

Member
Joined
Dec 13, 2016
Messages
12
Programming Experience
1-3
My C# code is returning the first phone number in the xml document regardless of its type.
But the requirement is that my C# code should return only one phone number based on type found in xml document in a certain order.
There are 4 types of phone numbers as you will see in the xml document code I have posted here below.
The condition to display the phone number by type is as follows:
1. Check in the xml for Phone Current ="true". After that display the phone number where Type Word ="HOME" first. If this type is not found, look for Type Word ="CELL" and display it. If this is not found then look for Type Word ="WORK" and display it. If all above Types are not found look for Type Word="FAX" and display the fax number.

xml document
C#:
<?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>

C# code
C#:
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Xml;
 namespace Msc.Integration.Mncis.Service.v4
 {
     class InsertUpdateCase : CamperUpdates
     {
         public void ProcessInsertUpdateCase(ref Msc.Integration.MessageBroker.Library.v4.Broker aobjBroker, ref System.Xml.XmlDocument aobjXmlInputDoc, ref Microsoft.VisualBasic.Collection aobjInstantiatedObjectsCollection, XmlNode aobjxmNotificationEventNode)
         /// <summary>
         /// </summary>
         /// <param name="objxmlBasePartyNode"></param>
         /// <param name="objxmlCasePartyNode"></param>
         /// <param name="objxmlBasePartyAddressNode"></param>
         /// <param name="objCaseParty"></param>
         protected void GetParty(ref XmlNode objxmlBasePartyNode, XmlNode objxmlCasePartyNode, ref XmlNode objxmlBasePartyAddressNode, ref ConservatorService.CaseParty objCaseParty, string astrDescription)
         {        
             objCaseParty.PartyId = objxmlBasePartyNode.SelectSingleNode("@ID").InnerText;
             objCaseParty.PartyType = (ConservatorService.MncisPartyTypes)Enum.Parse(typeof(ConservatorService.MncisPartyTypes), astrDescription);
             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 
        
         }
     }
 }
 
I think I would use an Xpath expression like this:
//Phone[@Current='true' and Type/@Word='HOME']

and go through the @Word options in order until a node was returned. When you have a Phone node you can get /Number and /Type from that.
 
How do I go through the @Word options in order until a node is returned to get the number and type from that? I tried the path but when there is no //Phone[@Current='true' and Type/@Word='HOME'] in xml, nothing is returned. What should happen in that case, is that use the //Phone[@Current='true' and Type/@Word='CELL' should be the number returned. If that does not exist then use //Phone[@Current='true' and Type/@Word='WORK'
 
I tried the path but when there is no //Phone[@Current='true' and Type/@Word='HOME'] in xml
clearly there is:
<Phone Current="true">
<Type Word="HOME">Home</Type>
<Number>218-300-0011</Number>
</Phone>
XmlNode objxmlPhoneNode = objxmlBasePartyNode.SelectSingleNode("Phone[@Current='true']");
Did you change this xpath to my example?
 
Ok, maybe I misunderstood your last post:
when there is no ..@Word='HOME'... in xml, nothing is returned
Yes, that is how it works. You check if the returned value is null, if that is the case you move on to next xpath where @Word='CELL'.
When returned value is not null you have a Phone node that you can query for Number (and Type).
 
Yes I did change the path to what you suggested. What I meant is that, I do not want this fax number returned
<Phone Current="true">
<Type Word="FAX">Fax</Type>
<Number>218-777-7777</Number>
</Phone>

When testing and I remove the <Type Word="HOME">Home</Type> from xml document I should get the Work type <Type Word="CELL">Cell</Type> because it has current. However I am not getting it returned. Nothing is returned
<Phone Current="true">
<Type Word="HOME">Home</Type>
<Number>218-300-0011</Number>
</Phone>

<Phone Current="true">
<Type Word="HOME">Home</Type>
<Number>218-300-0011</Number>
</Phone><Phone Current="true">
<Type Word="HOME">Home</Type>
<Number>218-300-0011</Number>
</Phone><><Phone Current="true">
<Type Word="HOME">Home</Type>
<Number>218-300-0011</Number>
</Phone>
 
Last edited:
You check if the returned value is null, if that is the case you move on to next xpath where @Word='CELL'. and so on.
 
I am already checking for null If(objxmlPhoneNode !=null){
I am still getting the type fax because it is the first one element in the xml document for the phone nodes. However I want to get the phone type home. The returned number should be for home type regardless of where the node is in the xml document.
And if home type with Current is not found, then I want to return Cell type. When I test without home type, nothing is returned.


 
Last edited:
I don't see the problem.
1. find HOME phone.
2. if noe HOME phone, find CELL phone. (same query, just with CELL)
3. etc according to your priorty list.
 
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.
Here is the output I expect
<Phone>
<Number>218-300-0011</Number>
<Type>Home</Type>
</Phone>
 
Last edited:
You just change the word HOME with CELL. I don't understand how you can't see that.
 

Latest posts

Back
Top Bottom