[英]I can not load a xml file in xamarin android
<?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>
當我啟動應用程序異常發生時,我將xml文件放置在Resources / drawable中,並在設置為Android Resources的狀態下建立操作。System.Xml.XmlException:根級數據無效。 第1行,第1.i位我不確定此異常意味着什么,請幫忙
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using Android.Content.Res;
using System.IO;
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);
string content;
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader(assets.Open("Q317664.xml")))
{
content = sr.ReadToEnd();
}
XmlDocument doc = new XmlDocument();
doc.LoadXml("content");
}
}
}
LoadXml方法將XML字符串作為參數。 您正在將字符串“ content”傳遞給方法,這會導致問題。 根據您的邏輯,應該
doc.LoadXml(content); //without ""
但是我也懷疑XML是否已正確加載此代碼。 如果這不起作用,則可以檢查變量內容的值(如果它已解析了XML字符串)。 有文檔如何加載文件作為嵌入資源由Xamarin。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.