简体   繁体   English

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

[英]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.

相关问题 解析 EntityName 时出错。 第 1 行,位置 844 - An error occurred while parsing EntityName. Line1, position 844 Winforms Designer错误“解析EntityName时发生错误” - Winforms Designer Error “An error occured while parsing EntityName” 解析 EntityName 时出错 - An error occurred while parsing EntityName 解析带有“&”的实体名称时发生错误 - an error occurred while parsing entityname with '&' IIS - 网站 - 浏览 - 解析错误消息:解析实体名称时出错:第 5 行位置 51 - IIS - Website - Browse - Parse Error Message: an error occurred while parsing entity name : line 5 position 51 XmlDocument抛出“解析EntityName时发生错误” - XmlDocument throwing “An error occurred while parsing EntityName” EntityName解析错误-XPathDocument - EntityName parsing error - XPathDocument 加载XmlDocument时“解析EntityName时发生错误” - “An error occurred while parsing EntityName” while Loading an XmlDocument 从有效的XML获取内容后,“解析EntityName时发生错误” - “An error occurred while parsing EntityName” after grabbing content from valid XML 反序列化JSON对象时发生错误“解析值时遇到意外字符:&lt;。 路径”,第0行,位置0。” - Error when deserializing JSON object “Unexpected character encountered while parsing value: <. Path '', line 0, position 0.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM