Sample XML -
<?xml version="1.0"?>
<Root>
<PhoneType dataType="string">
<Value>CELL</Value>
</PhoneType>
<PhonePrimaryYn dataType="string">
<Value>Y</Value>
</PhonePrimaryYn>
<PhoneNumber dataType="string">
<Value>555-555-5554</Value>
</PhoneNumber>
<PhonePrimaryYn dataType="string">
<Value>Y</Value>
</PhonePrimaryYn>
<PhoneType dataType="string">
<Value>HOME</Value>
</PhoneType>
<PhoneNumber dataType="string">
<Value>555-555-5555</Value>
</PhoneNumber>
</Root>
Without looping through each nodelist, can someone tell me (either with LINQ-to-XML or by other means) how I can perform the following?
In the XML sample you see two groups of 'PhoneType', 'PhonePrimaryYn', and 'PhoneNumber' type code groups. The first group of three relate to each other. The second group of three relate to each other as well, and so on.
Lets say I want to know what the cell phone number is.
I get that cell phone number when the 'PhoneType' 'Value' is 'CELL', the 'PhonePrimaryYn' 'Value' is 'Y', I get that 'PhoneNumber' 'Value' of '555-555-5554'. You get the idea hopefully.
I'd like to know if it is possible to get that 'PhoneNumber' value (cell phone for example) without having to loop through each nodelist group of the particular type.
Do anyone have any ideas?
UPDATE
Using an XDocument vs an XmlDocument, I believe this does what you're asking without using loops.
This is dependent on the elements being in the order of
<PhoneType> <PhonePrimaryYN> <PhoneNumber>
Results:
OLD ANSWER
When I loaded your sample XML into an
XmlDocument
I saw that it'sInnerText
property contained the following:From here, I thought Regex would be a good method for extracting CELL numbers using the pattern:
The pattern looks for the word "CELL", followed by a 'N' or 'Y', then the phone number in the format
###-###-#####
. The phone number is in a capture group and if a match is found it can be accessed like this following example illustrates.I added another CELL entry to show that you can get all CELL numbers in your XML. So the
InnerText
property, of theXmlDocument
, now looks likeCELLY555-555-5554YHOME555-555-5555CELLY555-555-9999
Results: