我需要使用XmlTextReader循環瀏覽XML文檔的節點。不幸的是,使用除XmlTextReader之外的其他任何東西都不是一種選擇。XmlTextReader - 如何遍歷節點
我的代碼:
class Program { private static void Main(string[] args) { XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=180491"); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Text: Console.WriteLine("Live: " + reader.Value); break; } } Console.ReadLine(); } }
XML used:
<own3dReply>
<liveEvent>
<isLive>true</isLive>
<liveViewers>225</liveViewers>
<liveDuration>1222</liveDuration>
</liveEvent>
</own3dReply>
What it's outputting to console:
Live: true Live: 225 Live: 1222
What it needs to output:
Live: true Viewers: 225 Duration: 1222
It needs to iterate through each node and do this, and I just can't figure it out. I tried using switch and while statements, but I just can't seem to get it to work.
出於興趣,*爲什麼*是除XmlReader之外的任何其他選項?在給出限制時,提供原因總是有用的,因爲它們會影響答案。 –
另外,不要使用'new XmlTextReader()'。使用'XmlReader.Create()'。 –
我想這是很好的處置它,所以使用使用:使用(var xtr = XmlReader.Create(uri)) –