如何从链接中提取正确的国家/地区代码和国家/地区名称?

时间:2016-12-28 09:01:36

标签: c# .net winforms

public static void ExtractCountires()
        {
            try
            {
                htmltoextract = "http://sat24.com/en/?ir=true";
                client = new WebClient();
                client.DownloadFile(htmltoextract, @"c:\temp\sat24.html");
                client.Dispose();

                string tag1 = "<li><a href=\"/en/";
                string tag2 = "</a></li>";

                string s = System.IO.File.ReadAllText(@"c:\temp\sat24.html");
                s = s.Substring(s.IndexOf(tag1));
                s = s.Substring(0, s.LastIndexOf(tag2) + tag2.ToCharArray().Length);
                s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");

                string[] parts = s.Split(new string[] { tag1, tag2 }, StringSplitOptions.RemoveEmptyEntries);


                string tag3 = "<li><ahref=\"/en/";

                for (int i = 0; i < parts.Length; i++)
                {
                    if (i == 17)
                    {
                        //break;
                    }
                    string l = "";
                    if (parts[i].Contains(tag3))
                        l = parts[i].Replace(tag3, "");

                    string z1 = l.Substring(0, l.IndexOf('"'));
                    if (!z1.Contains("</ul>"))
                    {
                        countriescodes.Add(z1);
                        countriescodes.GroupBy(n => n).Any(c => c.Count() > 1);
                    }

                    string z2 = parts[i].Substring(parts[i].LastIndexOf('>') + 1);
                    if (z2.Contains("&amp"))
                    {

                    }
                    countriesnames.Add(z2);
                    countriesnames.GroupBy(n => n).Any(c => c.Count() > 1);
                }
            }
            catch (Exception e)
            {

            }
        }

在第一个列表中的国家/地区代码中,它是原始的全局列表,我在索引17中编号为17:索引17中的国家/地区编号17我有非洲国家/地区

所以我不确定为什么在索引编号为17的国家/地区代码中我会得到

如果它的欧洲代码是欧元,或者国家名称是土耳其代码就是tu。

第二个问题出现在我所获得的两个地方的国家/地区名单中:

Spain&amp;Portugal

UK&amp;Ireland

并且

Romania&amp;Bulgaria

我想要的只是从这些项目中删除&amp;。 所以他们将是例如UK&amp;爱尔兰。

1 个答案:

答案 0 :(得分:2)

我做了一个简单的例子:

        string someCountryNames = "Ireland&amp;Brazil";
        if (someCountryNames.Contains("&amp;"))
        {
            someCountryNames = someCountryNames.Replace("&amp;", " & ");
        }

首先检查它是否包含您想要更改的值(在这种情况下为&amp; amp),如果是这样,您只需将其替换为如图所示。