2013-05-20 2 views
0

를 건너 뛰고, 그러나 그 모든은 (더블 reader.Read() 메소드) 해결 간단하지만 나의 아주 이상한 그런데, 내 jqplot 차트에 값 theese 있습니다 - elemName = "P"를 선택 http://chartapi.finance.yahoo.com/instrument/1.0/aapl/chartdata;type=quote;range=1m/XMLReader를 내가 비슷한 문제를 찾고 있었어요 모든 다른 노드

그리고 임 : 나는 URI 다음 구문 분석하려고

private IEnumerable<XElement> getXMLData(String inputURI, String elementName) 
    { 
     List<XElement> result = new List<XElement>(); 
     XmlReader reader = XmlReader.Create(inputURI); 
     XElement elem; 

     reader.MoveToContent(); 
     while (reader.Read()) 
     { 
      if (reader.NodeType == XmlNodeType.Element) 
      { 
       if (reader.Name.Equals(elementName)) 
       { 
        elem = XNode.ReadFrom(reader) as XElement; 
        if (elem != null) 
        { 
         result.Add(elem); 
        } 
       } 
      } 
     } 
     return result; 
    } 

: ASP.NET C#을하는 방법.

문제는이 파싱 메서드가 다른 모든 "p"노드를 나열하도록 저장된다는 것입니다. 이 XML 파일에는 20 개의 "p"노드가 있지만 내 파서는 10 개의 요소 만 저장합니다 (첫 번째부터 시작).

내가 뭘 잘못하고 있니?

답변

1

깔끔한 해결책은 다음과 같을 수 -

using (var client = new WebClient()) 
{ 
    string s = client.DownloadString("http://chartapi.finance.yahoo.com/instrument/1.0/aapl/chartdata;type=quote;range=1m/"); 
    var xDoc = XDocument.Parse(s); 
    var result = xDoc.Descendants("p").ToList(); 

    return result; 

} 

가 더 일반적인 만드는 방법에 전달 된 URL 문자열을 사용하여 URL을 교체합니다.

관련 문제