0

I have written the following to read an XML fragement from disk:

string fileName = @"C:\test.txt";
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlReader reader = XmlReader.Create(fileName, settings))
{
    while (reader.Read())
    { DoSomething(); }
}

But it fails when reading special characters like Ö, &, etc. I guess this is something with character encoding. I saw that I can do something like XmlReader.Create(fileName, fileEndoding). However, how do I combine this with the setting of a XMLFragment? My character encoding is ISO8859-1

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Martijn Burger
  • 7,315
  • 8
  • 54
  • 94
  • If your encoding is ISO-8859-1, the document should state that. Don't fix the reader - fix the document. – Jon Skeet Feb 23 '13 at 19:10
  • Sorry, fixing the document is not an option. – Martijn Burger Feb 23 '13 at 19:14
  • I suggest you don't try to use an XmlReader then - it's just not a valid XML document. Is this actually different documents at different times? What's producing them? (You shouldn't have to put up with broken XML. It's *easy* to produce valid XML, and anyone producing broken XML should be shouted at.) – Jon Skeet Feb 23 '13 at 19:21

1 Answers1

1

Try this: new StreamReader(fileName, Encoding.GetEncoding("ISO-8859-1"))

VAV
  • 1,756
  • 1
  • 16
  • 26
  • Works almost. It fails on this xml element: Some & Somemore Any workarounds for the ampersand??? – Martijn Burger Feb 23 '13 at 19:19
  • XmlReaderSettings.CheckCharacters property? – VAV Feb 23 '13 at 19:24
  • Nope, sorry. CheckCharacters to false does not work, changing the xml doc is not an option. Posted this problem as a new question here: http://stackoverflow.com/questions/15044771/c-sharp-xml-fragment-with-an-ampersand – Martijn Burger Feb 23 '13 at 19:43