[英]An Error occured while parsing EntityName. Line 32, position 51
I've tried looking at several different methods revolving around using XDocument class to load my xml file. 我尝试查看围绕使用XDocument类加载我的xml文件的几种不同方法。 However, this error and other variations have been appearing.
但是,此错误和其他变化已出现。 If I use the absolute path, it displays an error of it cannot find the file.
如果我使用绝对路径,则会显示错误消息,找不到文件。 The issue is my xml file has a combination of both English and Japanese used in it.
问题是我的xml文件同时使用了英语和日语。 The link should allow for anyone to view the xml file.
该链接应允许任何人查看xml文件。
Here is my code and xml file: 这是我的代码和xml文件:
public partial class MainWindow : Window
{
private string URLSource = "https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=0";
public MainWindow()
{
InitializeComponent();
XMLViewer();
}
private void XMLViewer()
{
try
{
XDocument Doc = XDocument.Load(URLSource);
var Kanji = from WordList in Doc.Descendants("Kanji")
select new
{
Word = WordList.Element("Kanji").Value
};
foreach (var Word in Kanji)
{
JpnTxt.ItemsSource = Word.ToString();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
} }
The URL you use don't contain a XML document but an HTML page : https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=0
您使用的URL不包含XML文档,而是包含HTML页面:
https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=0
: https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=0
You need to change the value of dl
to 1, so Dropbox will return your XML document: https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=1
您需要将
dl
的值更改为1,因此Dropbox将返回您的XML文档: https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=1
: https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=1
dl
https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=1
As @Florian said you should use second link, but maybe you have problem to read unicode xml, so its better using Request and ResponseStream: 正如@Florian所说的,您应该使用第二个链接,但也许您在读取unicode xml时遇到问题,因此最好使用Request和ResponseStream:
private string URLSource = "https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=1";
private void XMLViewer()
{
try
{
var request = (HttpWebRequest)WebRequest.Create(URLSource);
var response = (HttpWebResponse)request.GetResponse();
using (var stream = response.GetResponseStream())
{
using (var sr = new StreamReader(stream, true))
{
XDocument Doc = XDocument.Load(sr);
var Kanji = from WordList in Doc.Descendants("Kanji")
select new
{
Word = WordList.Element("Kanji").Value
};
foreach (var Word in Kanji)
{
JpnTxt.ItemsSource = Word.ToString();
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.