'How to read a XML file with a "Unexpected XML declaration"?
My code for reading an XML file in a zip file:
"ZipPathListSize" is the number of the zip files, @ZipPathList[i] is an string array has every zip file path, and the "modDesc / modDesc.xml" is the XML file's fixed name in every zip file.
using System.IO.Compression;
using System.Xml;
--------------------------------------------
for (int i = 0; i < ZipPathListSize; i++)
{
using (ZipArchive ZipFileContent = ZipFile.OpenRead(@ZipPathList[i]))
{
XmlDocument ModDesc = new();
ZipArchiveEntry Entry = ZipFileContent.GetEntry("modDesc.xml");
if (Entry != null)
{
using (Stream stream = Entry.Open())
{
bool ExFlag = false;
try
{
ModDesc.Load(stream);
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(@ZipPathList[i] + ex));
ExFlag = true;
}
finally
{
if (ExFlag == false)
{
XmlNode MVersion = ModDesc.DocumentElement.SelectSingleNode("/modDesc");
int Version = Convert.ToInt16(MVersion.Attributes["descVersion"].InnerText);
//MessageBox.Show(@ZipPathList[i] + Version); //DEBUG ONLY
}
}
}
}
else
{
MessageBox.Show("Not A Mod"); //DEBUG ONLY
}
}
}
The question is, some of the XML files (modDesc.xml) in zip files didn't have a correct declaration. Such as:
Empty first line
<!-- This is an empty line (Line 1), in the origin file -->
<?xml version="1.0" encoding="utf-8" standalone="no" ?> <!-- Here is Line 2 -->
<modDesc descVersion="61">
...
or Wrong version
<?xml version="1.1" encoding="utf-8" standalone="no" ?> <!-- 1.1? Really? -->
<modDesc descVersion="62">
but all other parts of those XMLs are correct.
How can I read an XML with the wrong declaration? Maybe somehow ignore the format checking and force it to read nodes and attributes?
By the way, I can not modify any XML files manually or fix them in a non-programming way.
So, no vscode, no notepad++ :(
System.Xml.XmlException: The XML declaration must be the first node in the document, and no whitespace characters are allowed to appear before it. Line 2, position 3.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParsePI(StringBuilder piInDtdStringBuilder)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(Stream inStream)
at FS22_ModManagerCore.ListMods.ByModFolder(String ModFolder) in
.....ListMods.cs:line 37
System.Xml.XmlException: Version number '1.1' is invalid. Line 1, position 16.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl)
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(Stream inStream)
at FS22_ModManagerCore.ListMods.ByModFolder(String ModFolder) in
......ListMods.cs:line 37
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
