简体   繁体   中英

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. 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. The link should allow for anyone to view the xml file.

Here is my code and xml file:

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

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

As @Florian said you should use second link, but maybe you have problem to read unicode xml, so its better using Request and 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);
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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