gpt4 book ai didi

c# - 根级别的数据无效。第 1 行,位置 1 MonoTouch

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:33 24 4
gpt4 key购买 nike

我正在尝试解析一些 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... &lt;br/&gt;
From JobHost - 21 Mar 2012 05:04:47 GMT
- View all &lt;a href=&#034;http://www.indeed.com/l-Andover,-MA-jobs.html&#034;&gt;Andover jobs&lt;/a&gt;
</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 处无效

关于c# - 根级别的数据无效。第 1 行,位置 1 MonoTouch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com