2009-12-23 2 views
0

이것이 재 게시임을 알고 있지만 LINQ를 사용할 수 없어서 필요했습니다 (이전 게시물에서 요청 됨)..net 2.0 C# XML에서 값 가져 오기

아래의 XML 문서에서 woeid를 읽어야합니다. yahoo 기상 서비스를 쿼리 할 수 ​​있도록 문자열 변수에 데이터를 읽고 저장해야합니다. 쿼리에 의해 반환

XML :

<query yahoo:count="1" 
     yahoo:created="2009-12-22T08:30:31Z" 
     yahoo:lang="en-US" 
     yahoo:updated="2009-12-22T08:30:31Z" 
     yahoo:uri="http://query.yahooapis.com/v1/yql?q=select+woeid+from+geo.places+where+text%3D%22farnborough%2C+greater+london%2Cuk%22"> 

<diagnostics> 
<publiclyCallable>true</publiclyCallable> 

<url execution-time="32"> 
http://where.yahooapis.com/v1/places.q(farnborough%2C%20greater%20london%2Cuk);start=0;count=10 
</url> 
<user-time>33</user-time> 
<service-time>32</service-time> 
<build-version>4265</build-version> 
</diagnostics> 

<results> 

<place> 
<woeid>19941</woeid> 
</place> 
</results> 
</query> 

누군가가 내가 XPath는 또는 .net 2.0의 라이브러리와 호환되는 다른 방법을 통해이 작업을 수행 할 수있는 방법을 제안 할 수 있습니다?

답변 해 주셔서 감사합니다. 따라서 나는 아래의 방법을 사용하고,하지만 난 XML 문서를로드 할 때 발생 예외가 얻을 :

private string getWOEID(string UserLocation, string UserCountry) 
{ 
    string woeID = ""; 

    if (UserLocation == "farnborough") { UserLocation = "farnborough" + "%2C" + "hampshire"; } 

    String reqUrl = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text%3D%22" + UserLocation + "%2C" + UserCountry + "%22&format=xml"; 

    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(reqUrl); 
    string woeid = doc.SelectSingleNode("query/results/place/woeid/text()").Value; 
    return woeID; 


} 
+0

수정 사항에 대한 업데이트 답변을 참조하십시오. – ChaosPandion

+0

고마워 ChaosPandion 나는 수정으로 재건. 그러나 예외는 여전히 doc.LoadXml (reqUrl) 문에서 발생합니다. { "1 단계, 위치 1의 루트 수준의 데이터가 유효하지 않습니다."} – van

+0

필요에 맞게 내 대답을 업데이트했습니다. LoadXml을 Load로 바꿨습니다. – ChaosPandion

답변

2

는 출발점으로 ChaosPandion의 게시물을 사용하여,이에 이동 할 것.

+0

GetElementsByTagName을 XPath 쿼리를 통해 사용할 때 성능 고려 사항은 무엇입니까? – ChaosPandion

+0

-1 'XmlTextReader'를 사용하는 경우 -1, 더 이상 사용되지 않습니다. 대신에'XmlReader.Create'를 사용하십시오. –

+0

@ 존 : 그는 네임 스페이스를 무시하기 위해서만 사용했습니다. XmlReader.Create를 사용하여이를 수행 할 수있는 방법이 있습니까? – Badaro

3

기존 방법 : 그 일의

XmlDocument doc = new XmlDocument(); 
doc.Load(url); 
string woeid = doc.SelectSingleNode("query/place/woeid/text()").Value; 

덜 기존의 방법 :

int start = xmlString.IndexOf("<woeid>") + 7; 
int end = xmlString.IndexOf("</woeid>", start); 
string woeid = xmlString.Substring(start, end - start); 

누구나 벤치 마크를 실행하고 싶습니까?

XmlDocument doc = new XmlDocument(); 
    using (XmlTextReader tr = new XmlTextReader(new StringReader(xmlString))) 
    { 
     tr.Namespaces = false; 
     doc.Load(tr); 
    } 
    string woeId = doc.GetElementsByTagName("woeid")[0].InnerText; 

공지 사항을 여기에 당신이 시도하고있는 무슨을 위해 그들을 필요로하지 않기 때문에 내가 일부러 여기 네임 스페이스를 무시하고있어 것을 : 나는 다른이있을 것이다

+1

-1은 문자열 조작을 제안합니다. –

+1

Mark가 3 분의 1을 제공하는 것처럼 2 가지 옵션을 제공합니다. 나는 이것이 투표율에 어울리는지를 보지 못한다. – ChaosPandion

+1

첫 번째 예제가 작동하지 않고 Xpath를 수정해야합니다. string woeId = doc.SelectSingleNode ("query/results/place/woeid/text()"). – Badaro