每当我想要加载XML文件时,都会发生异常:System.XML.XmlException:根级别的数据无效 相对性原理 爱因斯坦 物理 宇宙 卡尔·萨根 宇宙学

每当我想要加载XML文件时,都会发生异常:System.XML.XmlException:根级别的数据无效 相对性原理 爱因斯坦 物理 宇宙 卡尔·萨根 宇宙学,xml,xamarin.android,Xml,Xamarin.android,当应用程序在3秒后启动时,会发生此异常: System.Xml.XmlException:根级别的数据无效。第1行,位置1 为什么在该XML代码正确且正在控制台应用程序中加载时发生此异常,而在控制台应用程序中没有发生异常 <?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?> <Collection> <Book Id='1' ISBN='1-100000ABC-200'>

当应用程序在3秒后启动时,会发生此异常:

System.Xml.XmlException:根级别的数据无效。第1行,位置1

为什么在该XML代码正确且正在控制台应用程序中加载时发生此异常,而在控制台应用程序中没有发生异常

<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>
<Collection>
    <Book Id='1' ISBN='1-100000ABC-200'>
        <Title>Principle of Relativity</Title>
        <!-- Famous physicist -->      
        <Author>Albert Einstein</Author>
        <Genre>Physics</Genre>
    </Book>
    <Book Id='2' ISBN='1-100000ABC-300'>
        <!-- Also came as a TV serial -->
        <Title>Cosmos</Title>
        <Author>Carl Sagan</Author>
        <Genre>Cosmology</Genre>
    </Book>
    <!-- Add additional books here -->
</Collection>
使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用System.Xml;
使用System.Xml.Linq;
使用System.Linq;
名称空间App16
{
[活动(Label=“App16”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
整数计数=1;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
XmlDocument doc=新的XmlDocument();
doc.LoadXml(“Q317664.xml”);
//var doc=XDocument.Load(“Q317664.xml”);
//从布局资源中获取我们的按钮,
//并在其上附加一个事件
Button Button=FindViewById(Resource.Id.MyButton);
button.Click+=delegate{button.Text=string.Format(“{0}clicks!”,count++);};
}
}
}

提供的XML有错误。集合节点未正确结束

你能试试这个XML吗?并检查问题是否已解决

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

namespace App16
{
    [Activity(Label = "App16", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("Q317664.xml");
            // var doc = XDocument.Load("Q317664.xml");

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
        }
    }
}

相对性原理
爱因斯坦
物理
宇宙
卡尔·萨根
宇宙学

我检查了它,但同一个异常集合标记在我的xml文件中已正确关闭。很抱歉,这里缺少Collection closing tag@Jack Morton
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes'?>
<Collection>
    <Book Id='1' ISBN='1-100000ABC-200'>
    <Title>Principle of Relativity</Title>
    <!-- Famous physicist -->      
    <Author>Albert Einstein</Author>
        <Genre>Physics</Genre>
    </Book>
    <Book Id='2' ISBN='1-100000ABC-300'>
        <!-- Also came as a TV serial -->
        <Title>Cosmos</Title>
        <Author>Carl Sagan</Author>
        <Genre>Cosmology</Genre>
    </Book>
</Collection>