我正在尝试解析一些XML,但是,我得到的错误高于这句话。
这是我的代码:
class Program
{
static void Main(string[] args)
{
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?><rss version=""2.0"" xmlns:georss=""http://www.georss.org/georss"">
<channel>
<title>asp.net Jobs in Boston, MA | Indeed.com</title>
<link>http://www.indeed.com/q-asp.net-l-boston,-jobs.html</link>
<description>Indeed.com - one search. all jobs. Search thousands of sites for asp.net Jobs in Boston, MA</description>
<language>en</language>
<copyright>Copyright (c) 2012 Indeed, Inc All rights reserved.</copyright>
<lastBuildDate>Sun, 25 Mar 2012 19:43:15 GMT</lastBuildDate>
<image>
<url>http://www.indeed.com/images/indeed_rss.png</url>
<title>Indeed.com - one search. all jobs.</title>
<link>http://www.indeed.com/</link>
</image>
<item>
<title>Software Engineer with ASP.Net applications experience - The Integrity Group - Andover, MA</title>
<link>http://www.indeed.com/job/Software-Engineer-With-ASP-Net-Application-Experience-at-The-Integrity-Group-in-Andover,-MA-6ab16ba39cc13536</link>
<source>JobHost</source>
<guid isPermaLink=""false"">d61c3504e9df0fc13be4abb4be209c38</guid>
<pubDate>Wed, 21 Mar 2012 05:04:47 GMT</pubDate>
<description>layers and preferably service based architecture. ASP.Net web applications with server-side controls and... Solid experience in ASP.Net, SQL Server, C#, XML, XML... <br/>
From JobHost - 21 Mar 2012 05:04:47 GMT
- View all <a href="http://www.indeed.com/l-Andover,-MA-jobs.html">Andover jobs</a>
</description>
<georss:point>42.64835 -71.15934</georss:point>
</item>
</channel></rss>";
foreach (SavedJob sj in GetJobs(xml))
{
Console.WriteLine(sj.title);
}
}
public static List<SavedJob> GetJobs(string xml)
{
//http://forum.unity3d.com/threads/31314-Include-Files-in-build
XmlDocument xmlDoc = new XmlDocument();
System.IO.StringReader stringReader = new System.IO.StringReader(xml);
stringReader.Read();
//http://unity3d.qatohost.com/questions/161528/loading-a-large-xml-file-200-multi-level-nodes-int.html
// skip BOM
xmlDoc.LoadXml(stringReader.ReadToEnd());
List<SavedJob> SavedJobs = new List<SavedJob>();
try
{
foreach (XmlElement found in xmlDoc.GetElementsByTagName("item"))
{
SavedJob sj = new SavedJob();
sj.title = found.GetElementsByTagName("title")[0].InnerText;
sj.link = found.GetElementsByTagName("link")[0].InnerText;
sj.description = found.GetElementsByTagName("description")[0].InnerText;
DateTime dt = new DateTime();
DateTime.TryParse(found.GetElementsByTagName("pubDate")[0].InnerText, out dt);
sj.date = dt;
SavedJobs.Add(sj);
}
}
//data source is null
catch (Exception)
{
}
return SavedJobs;
}
}
public class SavedJob
{
public string title { get; set; }
public string link { get; set; }
public string description { get; set; }
public DateTime date { get; set; }
}
我的目标是在MonoTouch中创建一个加载面板,但是,我似乎无法下载字符串,因为XML很糟糕。有办法解决这个问题吗?
LoadingView lv = new LoadingView ();
WebClient wc = new WebClient ();
wc.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs e) {
// We got the async result now display data
InvokeOnMainThread (delegate {
if (e.Result != null) {
SavedJobs = basicOperations.GetJobs (e.Result);
TableView.Source = new DataSource (this);
TableView.ReloadData();
lv.Hide ();
}
});
};
lv.Show ("Loading");
wc.DownloadStringAsync (new Uri (uString));
答案 0 :(得分:2)
您的代码显示:
System.IO.StringReader stringReader = new System.IO.StringReader(xml);
stringReader.Read();
xmlDoc.LoadXml(stringReader.ReadToEnd());
这是做什么的:
所以你需要避免从xml中剥离第一个开口尖括号的初始stringReader.Read();
调用 - 使其在1,1
答案 1 :(得分:0)
更好的方法是从DownloadFileAsync创建一个文件:
WebClient wc = new WebClient();
string fPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/myfile.xml";
wc.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e) {
BasicOperations bas = new BasicOperations();
//save results as file
SavedJobs = bas.GetJobs(fPath);
TableView.Source = new DataSource (this);
TableView.ReloadData();
};
wc.DownloadFileAsync(new Uri(uString), fPath);