根级别的数据无效.线路1,位置1 MonoTouch
本文关键字:位置 MonoTouch 线路 无效 数据 | 更新日期: 2023-09-27 18:29:00
我正在尝试解析一些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));
您的代码显示:
System.IO.StringReader stringReader = new System.IO.StringReader(xml);
stringReader.Read();
xmlDoc.LoadXml(stringReader.ReadToEnd());
它的作用是:
- 在"上创建字符串读取器
- 然后读取第一个字符-"<"
- 然后尝试将其余字符加载为xml,即"xml…>"
因此,您需要避免最初的stringReader.Read();
调用,该调用会从xml中剥离第一个左括号,使其在1,1 中无效
一个更好的方法是从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);