2011-10-10 3 views
1

XML 문서를 구문 분석하여 "내용"노드의 첫 번째 항목을 추출하고 해당 노드의 내부 텍스트를 읽습니다.XML 문서에서 노드를 선택하는 문제 - NULL을 반환합니다.

에서 오는 XML 파일에서 볼 수있다 :

https://www.googleapis.com/shopping/search/v1/public/products?key=AIzaSyAfaLdRHGKa5Rens--Vpw-KftdzWyxe2co&country=GB&q=5055277014187&alt=atom

그 뒤의 코드를 처리하는 파일은 다음과 같습니다

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestToSend); 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
StreamReader sr = new StreamReader(response.GetResponseStream()); 
string responseString = sr.ReadToEnd(); 

XmlDocument thisXmlDoc = new XmlDocument(); 
thisXmlDoc.LoadXml(responseString); 

//get Companies 
XmlNodeList ocNodesCompany = thisXmlDoc.SelectNodes("//feed/entry/content"); 
foreach (XmlElement element in ocNodesCompany) 
{ 
    description = element.InnerText; 
    testOutput = "<b>" + stockRow["stockitem_text"].ToString() + "</b>: " + description + "<br /><br />"; 
    break; 
} 

Response.Write(responseString + "<br /><br />" + testOutput); 

불행하게도 XML 파일이 제대로 통해 오는 경우에도, 노드를 찾지 못하고 NULL을 반환하고 'foreach'루프에 들어 가지 않습니다.

도움 주셔서 감사합니다. you have to declare and use namespace prefix가 일부 네임 스페이스에있는 요소에 대한 XPath 쿼리를 사용하는 경우

+0

URL을 수정하고 키를 삭제해야한다고 생각합니다. –

답변

2

로 사용할 수 있습니다.

다음 코드는 작동 : 루트 노드를 참조 할 때

var nsmgr = new XmlNamespaceManager(new NameTable()); 
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); 

XmlNodeList ocNodesCompany = 
    thisXmlDoc.SelectNodes("/atom:feed/atom:entry/atom:content", nsmgr); 

또한, 당신은 / 대신 //을 사용해야합니다.

0

당신은 thisXmlDoc.GetElementsByTagName("content"); 대신 thisXmlDoc.SelectNodes("//feed/entry/content");

관련 문제