【问题标题】:When ever I want to load XML file exception occur: System.Xml.XmlException: Data at the root level is invalid当我想加载 XML 文件时发生异常:System.Xml.XmlException:根级别的数据无效
【发布时间】:2016-04-15 06:34:57
【问题描述】:
<?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>

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

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

为什么在此 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 xamarin.android


    【解决方案1】:

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

    你能试试这个 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>
    </Collection>
    

    【讨论】:

    • 我检查过了,但同样的异常 Collection 标签已在我的 xml 文件中正确关闭,抱歉这里缺少 Colllection 结束标签 @Jack Morton
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多