【问题标题】:Data at the root level is invalid. Line 1, position 1 MonoTouch根级别的数据无效。第 1 行,位置 1 MonoTouch
【发布时间】:2012-03-26 09:52:19
【问题描述】:

我正在尝试解析一些 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));

【问题讨论】:

  • 不要链接 XML,将其包含在您的帖子中
  • stringReader.Read(); 摆脱它。

标签: c# .net xml mono xamarin.ios


【解决方案1】:

您的代码显示:

    System.IO.StringReader stringReader = new System.IO.StringReader(xml);
    stringReader.Read();
    xmlDoc.LoadXml(stringReader.ReadToEnd());

这是做什么的:

  • 在“”上创建一个字符串阅读器
  • 然后读取第一个字符 - “
  • 然后尝试将剩余的字符加载为 xml - 即“xml ...>”

因此,您需要避免最初的 stringReader.Read(); 调用从 xml 中剥离第一个左尖括号 - 使其在 1,1 处无效

【讨论】:

    【解决方案2】:

    一个更好的方法是从 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2020-12-19
      • 2011-10-28
      相关资源
      最近更新 更多