我无法让.NET XML阅读器合作。这是我的代码:
public void downloadfile(string reqURL)
{
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(reqURL);
// Set some reasonable limits on resources used by this request
webreq.MaximumAutomaticRedirections = 4;
webreq.MaximumResponseHeadersLength = 4;
//====================
// Set credentials to use for this request.
webreq.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)webreq.GetResponse();
//====================
// Get the stream associated with the response.
XMLStream = response.GetResponseStream();
Console.WriteLine("Response gotten");
readStream = new StreamReader(XMLStream, Encoding.UTF8);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = System.Xml.XmlReader.Create(readStream, settings);
String title;
String link;
String descrip;
int cut;
for (; ; )
{
//get data
reader.ReadToFollowing("title");
title = reader.ReadInnerXml();
reader.ReadToFollowing("link");
link = reader.ReadInnerXml();
reader.ReadToFollowing("description");
descrip = reader.ReadInnerXml();
//check if end of file.
if (reader.EOF == true) { break; }
if (title != "SimpleVoiceCenter message")
{
String istribox;
istribox = descrip.Substring(0, 20);
if
(
istribox != "Voicebox number: 99." &&
istribox != "Voicebox number: 88." &&
istribox != "Voicebox number: 77."
)
{
//Title =====
Console.WriteLine("title: " + title);
//Link =====
Console.WriteLine("link: ...");
//Descrip =====
cut = descrip.IndexOf(".");
if (cut < 0)
{
cut = 20;
}
descrip = descrip.Remove(cut);
if (descrip.EndsWith("."))
{
descrip = descrip.Remove(cut - 1);
}
descrip = descrip.Replace("Voicebox number: ", " ");
descrip = descrip.Trim();
Console.WriteLine("voicebox: " + descrip);
Console.WriteLine("");
}
else { Console.WriteLine("skipping tribox"); }
} else { Console.WriteLine("skipping title");}
}
}
这是我的XML:
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>Simple Voice Center RSS feed</title>
<link>http://www.simplevoicecenter.com</link>
<description>This is the Simple Voice Center RSS feed. It allows an user to use the Podcasts technology to listen to voicebox messages.</description>
<lastBuildDate>Fri, 29 Jan 2010 07:25:25 AM EST</lastBuildDate>
<item>
<title>User greeting</title>
<guid>0d84c8d1c4021254d63d45b0e0e52cd7</guid>
<link>...</link>
<description>Voicebox number: 0. Greeting had been recorded on June 06 2008 03:32:25 PM EDT. Greeting duration 00:03:06. File size: 1.42 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>bec76518968e65447da7a1e0641b69d8</guid>
<link>...</link>
<description>Voicebox number: 0. Name had been recorded on January 06 2008 11:06:45 PM EST. Name duration 00:00:02. File size: 0.02 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>889a47cda06094d0b36e39af33399dd3</guid>
<link>...</link>
<description>Voicebox number: 1. Greeting had been recorded on September 19 2009 03:04:16 PM EDT. Greeting duration 00:02:16. File size: 1.04 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>cfd62ac51536fdbda52b4ddc37a3d58a</guid>
<link>...</link>
<description>Voicebox number: 1. Name had been recorded on October 19 2006 05:04:02 PM EST. Name duration 00:00:03. File size: 0.03 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>4242a9aaef7b78568d47895894290664</guid>
<link>...</link>
<description>Voicebox number: 2. Greeting had been recorded on July 09 2009 05:18:51 PM EDT. Greeting duration 00:07:30. File size: 3.44 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>4f3f85ac57a8834c816d840fef2c0a71</guid>
<link>...</link>
<description>Voicebox number: 2. Name had been recorded on October 19 2006 05:07:12 PM EST. Name duration 00:00:04. File size: 0.03 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>f07c6b35551aa83c0a023e475f81a0b0</guid>
<link>...</link>
<description>Voicebox number: 3. Greeting had been recorded on June 07 2007 12:23:32 PM EDT. Greeting duration 00:00:15. File size: 0.12 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>ad591ec5fe4e32a426c606a7bcc6b1e1</guid>
<link>...</link>
<description>Voicebox number: 3. Name had been recorded on October 19 2006 05:09:02 PM EST. Name duration 00:00:03. File size: 0.03 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>2ea4df7f288289b9e8f92b3d7b485dc6</guid>
<link>...</link>
<description>Voicebox number: 4. Greeting had been recorded on June 07 2007 12:26:54 PM EDT. Greeting duration 00:00:33. File size: 0.26 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>a61975f414447c852e85e2b83a4eeabc</guid>
<link>...</link>
<description>Voicebox number: 4. Name had been recorded on October 19 2006 05:10:12 PM EST. Name duration 00:00:02. File size: 0.02 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>f7ee359afb2e2cc583df3d5c1b17fae4</guid>
<link>...</link>
<description>Voicebox number: 5. Name had been recorded on June 07 2007 01:18:37 PM EDT. Name duration 00:01:04. File size: 0.50 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>15b0439f475f32f72b0bcb66d5f1d3ad</guid>
<link>...</link>
<description>Voicebox number: 6. Name had been recorded on June 07 2007 01:37:40 PM EDT. Name duration 00:00:21. File size: 0.17 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>fcd6e34fdd010f2a7e71f0d75978a36a</guid>
<link>...</link>
<description>Voicebox number: 6. Message had been recorded on December 30 2008 05:16:25 AM EST. Message duration 00:03:15. File size: 1.49 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>2b9dafd1e24e709830d5f337f38c147d</guid>
<link>...</link>
<description>Voicebox number: 6. Message had been recorded on August 27 2009 04:39:10 PM EDT. Message duration 00:00:29. File size: 0.22 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>e0ba0d02cb2592f0fafe9e1bf467a267</guid>
<link>...</link>
<description>Voicebox number: 6. Message had been recorded on August 13 2008 08:22:20 PM EDT. Message duration 00:00:18. File size: 0.14 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>603a1c9916261d4231d87cd007f1423a</guid>
<link>...</link>
<description>Voicebox number: 6. Message had been recorded on August 13 2008 08:20:45 PM EDT. Message duration 00:00:26. File size: 0.20 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>c3097857ad2b1b9422605d7efb4e04a4</guid>
<link>...</link>
<description>Voicebox number: 7. Name had been recorded on June 07 2007 01:40:18 PM EDT. Name duration 00:00:12. File size: 0.10 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>7eff552b8c06ae5544fbe036e4691f0c</guid>
<link>...</link>
<description>Voicebox number: 8. Name had been recorded on July 09 2009 05:15:33 PM EDT. Name duration 00:00:23. File size: 0.18 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User name</title>
<guid>5adc9ad51f5c693d519fbf52d6cebc78</guid>
<link>...</link>
<description>Voicebox number: 9. Name had been recorded on October 11 2006 05:49:18 PM EST. Name duration 00:00:00. File size: 0.01 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>90e641d04c6a1b7e5db8b82e56dca2ac</guid>
<link>...</link>
<description>Voicebox number: 99. Greeting had been recorded on September 21 2009 10:22:07 AM EDT. Greeting duration 00:00:29. File size: 0.22 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>c70337321783674e252a9a9ecc562053</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on January 08 2010 07:54:17 PM EST. Message duration 00:00:07. File size: 0.05 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>7d71887b4b401448c616f678f37e5d72</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on June 20 2009 05:02:06 PM EDT. Message duration 00:01:50. File size: 0.84 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>1bace556c868965f80d86c3c5cf26728</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on August 29 2009 09:43:03 PM EDT. Message duration 00:00:47. File size: 0.36 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>93e5560aef94f84cb04cf158bf5e7a9a</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on July 16 2009 05:31:31 AM EDT. Message duration 00:00:26. File size: 0.20 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>8d14eb04665fd48fb2567be75440e9d7</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on June 20 2009 04:58:54 PM EDT. Message duration 00:00:55. File size: 0.42 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>cfb0843a7d7bc22fc6771553b07d4c42</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on June 14 2009 09:54:59 AM EDT. Message duration 00:01:38. File size: 0.75 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>d563663a620fefa507a5add7fa5348a1</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on January 05 2009 04:11:41 PM EST. Message duration 00:01:05. File size: 0.50 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>a680314acbc74572c1a6d5b514fe2c42</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on September 05 2008 09:30:41 AM EDT. Message duration 00:01:18. File size: 0.60 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>f99b55053d0da8a4f1e475af45f9abaf</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on August 22 2008 11:48:11 PM EDT. Message duration 00:01:01. File size: 0.47 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>83c17b59f0c9da70265a96f19c44a306</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on August 20 2008 11:49:31 AM EDT. Message duration 00:00:26. File size: 0.20 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>134b015a223f01d973956ad1678cd666</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on August 19 2008 10:07:07 PM EDT. Message duration 00:00:04. File size: 0.03 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>bd290e914806a29f9f9c53758e4adfbc</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on July 23 2008 12:21:49 AM EDT. Message duration 00:01:06. File size: 0.51 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>6d11dd122a7ca9f82a9b6117e011fe28</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on June 19 2008 10:22:51 PM EDT. Message duration 00:01:17. File size: 0.59 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>e8ffc169d93d0dce22c0cc5e63084326</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on May 05 2008 12:35:28 PM EDT. Message duration 00:00:33. File size: 0.26 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>e1346a4e13987a5966a0abb3359a30d3</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on December 02 2007 07:10:32 PM EST. Message duration 00:00:26. File size: 0.20 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>13dd8477337077e039194414acfa6e64</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on September 02 2007 02:21:50 PM EDT. Message duration 00:01:42. File size: 0.78 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>SimpleVoiceCenter message</title>
<guid>f9b5b040eeea90262712f3e6a94fb980</guid>
<link>...</link>
<description>Voicebox number: 99. Message had been recorded on September 01 2007 03:45:41 PM EDT. Message duration 00:00:34. File size: 0.27 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>07d99563b8a253804dfd663851cf3dfb</guid>
<link>...</link>
<description>Voicebox number: 88. Greeting had been recorded on September 21 2009 10:22:28 AM EDT. Greeting duration 00:01:31. File size: 0.70 MB.</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>a28645b32628cc854e3c978003217f1f</guid>
<link>...</link>
<description>Voicebox number: 77. Greeting had been recorded on September 21 2009 10:22:40 AM EDT. Greeting duration 00:00:48. File size: 0.37 MB.</description>
<author>Free Conference Call</author>
</item>
</channel>
</rss>
这是我的控制台输出:
Response gotten
title: Simple Voice Center RSS feed
link: ...
voicebox: 0
title: User greeting
link: ...
voicebox: 1
title: User greeting
link: ...
voicebox: 2
title: User greeting
link: ...
voicebox: 3
title: User greeting
link: ...
voicebox: 4
title: User name //This should be here but is not
link: ... //
voicebox: 5 //
title: User name
link: ...
voicebox: 6
skipping title
skipping title
title: User name //This should also be here, but is not
link: ... //
voicebox: 7 //
title: User name
link: ...
voicebox: 8
skipping tribox
skipping title
skipping title
skipping title
skipping title
skipping title
skipping title
skipping title
skipping title
skipping title
Done
更新:有没有人有其他想法?
答案 0 :(得分:1)
为什么要注意使用XML序列化。循环节点似乎不是处理这个问题的正确方法。 尝试为您需要的信息创建一个类(快速模型):
[Serializable]
[XmlRoot("rss")]
public class MyRss
{
[XmlAttribute("version")]
public string Version { get; set; }
[XmlElement("channel")]
public Channel Channel { get; set; }
...
}
[Serializable]
public class Channel
{
[XmlElement("version")]
public string Title { get; set; }
[XmlArrayItem("item")]
public List<Item> Items { get; set; }
...
}
然后将字符串反序列化为此类并使用它来创建输出:
public static MyRss Deserialize(string xmlString)
{
var obj = new XmlSerializer(typeof(MyRss)).Deserialize(new StringReader(xmlString));
return (MyRss)obj;
}
然后使用此对象输出您需要的内容。
希望这有帮助!
答案 1 :(得分:1)
对于解析和读取XML,LinqToXml是对XmlReader的一个很大的改进。