2013-02-09 43 views
1

這可能很簡單,但我似乎無法找到一種方法來做到這一點。如何從大字符串中返回一個字

我正在使用必應地圖服務從lat/long獲取城市名稱。

它給了我,我已經下載了像這樣的字符串大量的XML:

<Name> 
High Street, Lincoln, LN5 7 
</Name> 
<Point> 
<Latitude> 
53.226592540740967 
</Latitude> 
<Longitude> 
-0.54169893264770508 
</Longitude> 
</Point> 
<BoundingBox> 
<SouthLatitude> 
53.22272982317029 
</SouthLatitude> 
<WestLongitude> 
-0.55030130347707928 
</WestLongitude> 
<NorthLatitude> 
53.230455258311643 
</NorthLatitude> 
<EastLongitude> 
-0.53309656181833087 
</EastLongitude> 
</BoundingBox> 
<EntityType> 
Address 
</EntityType> 
<Address> 
<AddressLine> 
High Street 
</AddressLine> 
<AdminDistrict> 
England 
</AdminDistrict> 
<AdminDistrict2> 
Lincs 
</AdminDistrict2> 
<CountryRegion> 
United Kingdom 
</CountryRegion> 
<FormattedAddress> 
High Street, Lincoln, LN5 7 
</FormattedAddress> 
<Locality> 
Lincoln 
</Locality> 
<PostalCode> 
LN5 7 
</PostalCode> 
</Address> 

有剛開的城市名稱是在這兩個地方標記之間的簡單方法?

+0

正則表達式是你的朋友,例如:(。*?)@」 \ n \ n <\/Locality>「 – reinder 2013-02-09 13:15:48

+0

嗨後退,該正則表達式不起作用 – samil90 2013-02-09 13:33:50

回答

0

一個簡單的方法來解析那種串的 試試這個

const string HTML_TAG_PATTERN = "<.*?>"; 

static string StripHTML(string inputString) 
     { 
      return Regex.Replace 
       (inputString, HTML_TAG_PATTERN, string.Empty); 
     } 

調用它是通過使用string.IndexOf方法

// I have saved your xml in this file to test 
string xmlResult = File.ReadAllText(@"D:\temp\locality.txt"); 

int startPos = xmlResult.IndexOf("<Locality>"); 
int endPos = xmlResult.IndexOf("</Locality>"); 

if(endPos != -1 && startPos != -1) 
{ 
    string result = xmlResult.Substring(startPos + 10, endPos-startPos-10).Trim(); 
    Console.WriteLine(result); 
} 

的搜索術語<Locality>,然後搜索術語</Locality>。如果在字符串中找到術語,則使用Substring方法提取所需的部分。 (10是<Locality>的長度)

附註。儘管您的示例非常簡單,但使用正則表達式解析XML或HTML文件是一種不好的做法。雖然與您的問題沒有嚴格關聯,但是this famous answer(SO最常見的一個)解釋了爲什麼使用正則表達式來解析非常規語言並不是一個好主意。

如果你有一個問題,在正則表達式之後你會遇到兩個問題。

+0

這工作完美,謝謝你這麼多! – samil90 2013-02-09 13:48:29

+0

-1用於使用字符串操作來解析xml。 – 2013-02-09 19:44:25

0

您可以通過將常量字符串變量用作正則表達式的字符串來實現此目的。要得到這個城市的名字

string cityname = StripHTML(the code); 
+0

嗨nrsharma,謝謝你的回覆。我不熟悉RegEx,該模式不起作用,只是返回整個String返回 – samil90 2013-02-09 13:36:34

+0

您必須循環訪問xml節點,然後將值傳遞給函數StripHTML(值)一個接一個。它會給你確切的價值。 – nrsharma 2013-02-11 03:53:55

3

我其實很驚訝人們在這裏使用正則表達式和像indexOf這樣的東西。如果你像這樣處理XML,你可能會遇到一個令人討厭的驚喜或兩個驚喜。如果Bing決定開始使用CData。

.NET幸好也有XML,這是一樣易於使用的相當不錯的支持,所以我一直使用:

XmlDocument doc = new XmlDocument(); 
doc.LoadXml(xml); 
var nav = doc.CreateNavigator(); 
var iterator = nav.Select(@"//Locality"); 
while (iterator.MoveNext()) 
{ 
    Console.WriteLine("{0}", iterator.Current.InnerXml.Trim()); 
} 

請注意,你可能需要聲明一個命名空間的解析器Bing使用的xmlns。由於我沒有這部分XML,所以在這個例子中我不能添加它,但是這些東西很容易添加。

0

我也建議您爲此使用正確的XML解析。但是請注意,您提供的XML不適合用作XML文檔,因爲它具有多個根節點。不過,這很容易解決。

如果您使用XML解析,您將可以輕鬆地獲取所有其他數據,而無需任何煩人的解析。

這是很容易做的,所以比滾動你自己的XML解析代碼如果您真的應該使用它更強大:

這裏是它假定你的XML是在一個行示例字符串變量稱爲XML:

string locality = XElement.Load(new StringReader("<Root>"+xml+"<Root>")).XPathSelectElement("Address/Locality").Value.Trim(); 

下面是一個適當的例子:

using System; 
using System.IO; 
using System.Xml.Linq; 
using System.Xml.XPath; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Fix original XML, which has multiple root nodes! 
      // We fix it just by enclosing it in a root level element called "Root": 

      string xml = "<Root>" + originalXml() + "</Root>"; 

      // Read the XML as an XML element. 

      var xElement = XElement.Load(new StringReader(xml)); 

      // Easily access 'Locality' or any other node by name: 

      string locality = xElement.XPathSelectElement("Address/Locality").Value.Trim(); 
      Console.WriteLine("Locality = " + locality); 
     } 

     // Note: This XML isn't well-formed, because it has multiple root nodes. 

     private static string originalXml() 
     { 
      return 
@"<Name> 
High Street, Lincoln, LN5 7 
</Name> 
<Point> 
<Latitude> 
53.226592540740967 
</Latitude> 
<Longitude> 
-0.54169893264770508 
</Longitude> 
</Point> 
<BoundingBox> 
<SouthLatitude> 
53.22272982317029 
</SouthLatitude> 
<WestLongitude> 
-0.55030130347707928 
</WestLongitude> 
<NorthLatitude> 
53.230455258311643 
</NorthLatitude> 
<EastLongitude> 
-0.53309656181833087 
</EastLongitude> 
</BoundingBox> 
<EntityType> 
Address 
</EntityType> 
<Address> 
<AddressLine> 
High Street 
</AddressLine> 
<AdminDistrict> 
England 
</AdminDistrict> 
<AdminDistrict2> 
Lincs 
</AdminDistrict2> 
<CountryRegion> 
United Kingdom 
</CountryRegion> 
<FormattedAddress> 
High Street, Lincoln, LN5 7 
</FormattedAddress> 
<Locality> 
Lincoln 
</Locality> 
<PostalCode> 
LN5 7 
</PostalCode> 
</Address>"; 
     } 
    } 
}