[英]How to return one word from a large String
這可能很簡單,但我似乎找不到這樣做的方法。
我正在使用Bing地圖服務從lat / long獲取城市名稱。
它給了我大量的XML,我已經像這樣下載了一個String:
<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>
是否有一種簡單的方法來獲取兩個地點標簽之間的城市名稱?
我真的很驚訝人們在這里使用正則表達式和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,我不能在這個例子中添加,但這些東西很容易添加。
您可以通過將常量字符串變量用作正則表達式的字符串來實現此目的。 嘗試這個
const string HTML_TAG_PATTERN = "<.*?>";
static string StripHTML(string inputString)
{
return Regex.Replace
(inputString, HTML_TAG_PATTERN, string.Empty);
}
稱之為你想要獲得城市名稱的地方
string cityname = StripHTML(the code);
解析這種字符串的一種簡單方法是使用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文件是一種不好的做法。 雖然與您的問題沒有嚴格的關聯,但這個着名的答案 (最常見的SO之一)解釋了為什么使用Regex解析非常規語言不是一個好主意。
如果您遇到一個問題,在Regex之后您將遇到兩個問題。
我還建議您使用適當的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>";
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.