I am trying to load an XHTML 1.1 document using XDocument
(LINQ-to-XML) in a portable class library, but am receiving an exception where it can't parse the entities:
System.Xml.XmlException: Reference to undeclared entity 'nbsp'. Line 10, position 23.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.HandleGeneralEntityReference(String name, Boolean isInAttributeValue, Boolean pushFakeEntityIfNullResolver, Int32 entityStartLinePos)
at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos)
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)
at ShatteredTemple.EpubTweaker.EpubTweaker.LoadXDocumentFromZipFile(ZipFile epub, ZipEntry zipEntry) in [path omitted]\EpubTweaker.cs:line 159
(SO wouldn't let me post that in <pre>
tags - apologies for the faux code formatting.)
Normally I would try embedding the DTD per this question. However, that requires creating a custom XmlResolver
, which does not seem to be available to a PCL.
Using XmlReaderSettings
to try to disable CheckCharacters
and DtdProcessing
doesn't do anything.
Any suggestions for how I can handle "unknown" entities when parsing XML inside a PCL? I'd prefer to keep using LINQ-to-XML/XDocument
, but am potentially open to other XML parsers, as long as they can add an XML declaration (<?xml version="1.0" encoding="utf-8"?>
) to the document if one is missing.