Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am working on a program that needs to lookup information in a XML file, formatted like this: http://www.hrdsoftwarellc.com/hrdxmlcountries/LogbookCountryDataEx.xml

The tag "PrefixList" is the field I intend to use and my problem is to find a nice way to do it.

My program need to find out from which country a radio amateur call sign is located.

I the call sign is located in Denmark the PrefixList tag is:
PrefixList="5[PQ].*|O[UVZ].*"

Call sign examples: OZ1BV, 5Q5P, 5P9XYZ, UO9S, OV5RT, ect.

If the call sign is located in Germany the PrefixList tag is:
PrefixList="D[A-R].*"

Call sign examples: DA7T, DB4TY, DC2EW, DR8RE, ect.

Canada:
PrefixList="C[FGJK].*|C[HY][12].*|C[IZ][012].*|V[ABCEGX].*|V[DO][12].*|V[FY][012].*|X[JN][12].*|X[KO][012].*|X[LM].*"

Can this problem solved using a regular expression?

Best regards, Brian
Posted
Comments
Zoltán Zörgő 6-Sep-15 16:22pm    
I don't get your problem: these PrefixList attributes contain valid regular expressions. I suppose, you would like to match them against UniqueCalls001.item.Callsign attributes.
Just do it. What you want exactly to achieve? Where are you stuck?

1 solution

Something like this:
C#
var xml = XDocument.Load(@"http://www.hrdsoftwarellc.com/hrdxmlcountries/LogbookCountryDataEx.xml");

var cs = from c in xml.Root.Elements("Countries001").Elements("item")
         select new
         {
         Latitude=c.Attribute("Latitude").Value,
         Longitude=c.Attribute("Longitude").Value,
         re = new Regex(c.Attribute("PrefixList").Value, RegexOptions.IgnoreCase)
         };

var us = from u in xml.Root.Elements("UniqueCalls001").Elements("item")
         let c = cs.FirstOrDefault( x=> x.re.IsMatch(u.Attribute("Callsign").Value))
         select new
         {
         CallSign = u.Attribute("Callsign").Value,
         Latitude = c.Latitude,
         Longitude = c.Longitude
         };

us.Dump();
 
Share this answer
 
v2
Comments
Member 9584445 7-Sep-15 15:38pm    
Hi Zoltán and thanks for Your solution

I am not able to figure out how to use it. What I need is to send a callsign (eg. OZ1BV, to a function and get the country returned, in this case Denmark.

Best regards, Brian
Zoltán Zörgő 7-Sep-15 15:40pm    
That's even simpler.

var xml = XDocument.Load(@"http://www.hrdsoftwarellc.com/hrdxmlcountries/LogbookCountryDataEx.xml");

var cs = from c in xml.Root.Elements("Countries001").Elements("item")
let re = new Regex(string.Format("^{0}$",c.Attribute("PrefixList").Value), RegexOptions.IgnoreCase)
where re.IsMatch("OZ1BV")
select c.Attribute("Country").Value;

cs.Dump();

I hope you can wrap a class or at least a method around it yourself.
But have you noticed that UniqueCalls001.item elements already have country attribute?
Member 9584445 8-Sep-15 6:42am    
Yes, I will wrap my own class around it.
UniqueCalls001 is a very special part only containing a few callsigns.
Member 9584445 8-Sep-15 8:16am    
btw: Forgot to say thanks a lot - it's working perfect :-)
Zoltán Zörgő 8-Sep-15 9:39am    
You are welcome

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