繁体   English   中英

解析EntityName时发生错误。 第32行,位置51

[英]An Error occured while parsing EntityName. Line 32, position 51

我尝试查看围绕使用XDocument类加载我的xml文件的几种不同方法。 但是,此错误和其他变化已出现。 如果我使用绝对路径,则会显示错误消息,找不到文件。 问题是我的xml文件同时使用了英语和日语。 该链接应允许任何人查看xml文件。

这是我的代码和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);
        }
    }
}

}

您使用的URL不包含XML文档,而是包含HTML页面: https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=0 : https://www.dropbox.com/s/nh3bfzvhpj6e3x1/JapanseEnglish.xml?dl=0

您需要将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

正如@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM