Hi all,

XMLDocument.Load("http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=Zlad!); If you open the link in FF it shows the error, IE seems blisfully unaware

Its failing due to Last.FM outputting artist names with the wrongly encoded characters into the xml.

This causes my C# app to crash. XMLException, invalid character

How would I go about making it work? Last.FM realise theres an issue but a fix is a long way off. Can I force it to ignore the invalid character? Best idea be catch the error, read in the page as a string instead, and then strip the invalid chars, and then load the xmldocument from there?

is it a webservice? you cant load xml from a URL i guess.

Its not a web-service.

It works perfectly on 99.9% of cases, however with the particular URL up there, searching for Zlad! it returns a result with a invalid char, need to get around the invalid char somehow

It looks like a web service, its asking for an API Key when trying to access that link. Not sure though.

Use try...catch, you might be able to find out which exact character is invalid which may lead to a clue.

Post a sample XML file here. It requires an API key to get the information and I don't feel like signing up :)

You can scrub the file before you load the data in to a DOM.

You can take a look at the XML specification to get a list of all non-readable ranges and scrub the entire range. This scrubs the specific characters that were a problem in the sample you posted:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace daniweb
{
  public partial class frmXmlScrub : Form
  {
    const string fName = @"C:\artist.xml";

    public frmXmlScrub()
    {
      InitializeComponent();
    }

    //Download the file, save it as text
    private void button1_Click(object sender, EventArgs e)
    {

      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(@"http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=Zlad!&api_key=b25b959554ed76058ac220b7b2e0a026");
      req.Method = "GET";
      HttpWebResponse res = (HttpWebResponse)req.GetResponse();
      using (StreamReader sr = new StreamReader(res.GetResponseStream()))
      {
        string xmlData = sr.ReadToEnd();
        xmlData = xmlData.Replace("\n", Environment.NewLine);
        if (File.Exists(fName))
          File.Delete(fName);
        File.WriteAllText(@"C:\artist.xml", xmlData);
      }
    }

    //scrub and read it
    private void button2_Click(object sender, EventArgs e)
    {
      List<char> charsToSubstitute = new List<char>();
      charsToSubstitute.Add((char)0x19);
      charsToSubstitute.Add((char)0x1C);
      charsToSubstitute.Add((char)0x1D);

      string fileText = File.ReadAllText(fName);
      foreach (char c in charsToSubstitute)
        fileText = fileText.Replace(Convert.ToString(c), string.Empty);

      XmlDocument doc = new XmlDocument();

      using (MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(fileText)))
      {
        XmlTextReader reader = new XmlTextReader(ms);
        try
        {
          while (reader.Read())
          {
            Console.WriteLine(reader.LineNumber);
          }
        }
        catch (XmlException ex)
        {
          Console.WriteLine(ex.Message);
          System.Diagnostics.Debugger.Break();
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.Message);
          System.Diagnostics.Debugger.Break();
        }
      }
    }
  }
}
commented: powered by sknake! +2

Thanks sknake, thats essentially what I was expecting to have to do.

Cheers for the help all

where Scott finds this energy from is the matter of question now.

where Scott finds this energy from is the matter of question now.

I know what you mean... Everytime I walk into Circle-K or 7-Eleven, I look at the counter and expect to find an "Energy drink powered by SKNAKE"--:D

I know what you mean... Everytime I walk into Circle-K or 7-Eleven, I look at the counter and expect to find an "Energy drink powered by SKNAKE"--:D

he can make good money if he plays in energy drink advertisements :)

Its all about MONSTER ENERGY. I love that stuff

[edit]
I just saw the commend from your +rep DdoubleD and it made lol :P
[/edit]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.