0

I'm trying to load XML without root element. I want to use LINQ to XML.

Following answer: https://stackoverflow.com/a/18186317/16514891 i recreated this code:

var doc = File.ReadAllText(Path.Combine(Settings.GameFolderPath, path));  
var rootedDoc = "<root>" + doc + "</root>";
XDocument correctDoc = XDocument.Parse(rootedDoc);

But i get XMLException "Unexpected XML declaration" on XDocument.Parse(), Line 1, Position 9. Using File.WriteAllText(path, rootedDoc) i know that first line looks like expected: <root><?xml version="1.0" encoding="utf-16"?>. It had to work somehow in answer i linked, but i don't understand how, how do you keep declaration first after wrapping original file?

Helios21cz
  • 27
  • 5
  • This is not a valid XML document. The XML declaration `?xml version="1.0" encoding="utf-16"?>` must be at the top of the document, befiore the `` – Klaus Gütter Sep 28 '21 at 12:43
  • XML Version 1.0 states that the XML declaration is **optional**, meaning the linked answer just doesn't care if a XML file contains it or not. But since your XML does contain it, it throws an exceptino, because if it is present it **must** be at the top of the document. The easiest way is probably to just remove everything between `xml.IndexOf("")` and `xml.IndexOf("?>")` – MindSwipe Sep 28 '21 at 12:43
  • Ah ok, thank you. So i simply removed declaration from original file before wrapping. Seems to be working, in worst case i can add declaration to start of new file. – Helios21cz Sep 28 '21 at 12:50

1 Answers1

0

This is because XML Version 1.0 specifies that the XML declaration (the <? ... ?> part) is optional, meaning, the linked answer probably just ignored it, or just wasn't tested with XML that does contain it. The easiest part is to just remove that XML declaration before parsing the file, like so:

xml = xml.TrimStart("<?xml version=\"1.0\" encoding=\"utf-16\"?>");
xml = $"<root>{xml}</root>";

If the XML is manually created/ can be manually edited before passing it to the code, you can always just manually remove it, although I wouldn't recommend it, as then your program would crash when ingesting (otherwise*) valid XML files.


*XML files with multiple root elements are technically already not valid XML files

MindSwipe
  • 7,193
  • 24
  • 47
  • Thanks. It would work, but XML i work with is cursed. When i load it like that, i get many exceptions because mistakes in XML file itself (mistyping, duplicates..). When i try to use custom `XMLReader` to hopefuly avoid some exception with `XMLReader.Settings`, i get exception "There is no Unicode byte order mark". I should never attempt to learn coding with strangers input files i guess. – Helios21cz Sep 28 '21 at 14:55
  • Well, the "no byte order mark" is easily fixable and has nothing to do with XML, it's referring to the encoding of the file, specifically that it lacks the [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) (BOM) required to figure out the encoding of the file. To solve it you can open then file, and "save as" and choose "UTF-8 BOM" in the "Encoding" drop down – MindSwipe Sep 29 '21 at 19:40