0

I am trying to read an XML file containing special characters:

<measValue measObjLdn="SSar-Oil-&-Gas, 564DC5">
     <measResults>1 0 0 100 0 1 </measResults>
</measValue>

Using the below logic, where stream is coming from an xml file:

        using (var xr = new XmlTextReader(stream))
        {
            xr.Namespaces = (NamespaceManager != null);
            xr.DtdProcessing = DtdProcessing.Ignore;

            while (xr.Read())
            {
                if (xr.NodeType == XmlNodeType.Element && arrTag.Contains(xr.Name))
                {
                    var node = (XElement)XNode.ReadFrom(xr);
                    yield return node;
                }
            }
            xr.Close();
        }

However it's throwing an error with the below error

An error occurred while parsing EntityName

I know the "&" isn't a valid character, but I'm not generating the file, is there any way to ignore these special characters?

dbc
  • 104,963
  • 20
  • 228
  • 340
Elias Ghali
  • 823
  • 1
  • 13
  • 29
  • Not with any of MS's `XmlReader` subclasses. That XML is malformed, and `XmlReader` and `XmltextReader` are designed only to parse well-formed XML. For confirmation, upload it to https://www.xmlvalidation.com/ and you will get an error, *`1: 34 The entity name must immediately follow the '&' in the entity reference.`*. – dbc Oct 25 '21 at 17:18
  • You might try HTML Agility Pack as recommended in [this answer](https://stackoverflow.com/a/996577/3744182) by Marc Gravell to [Parse malformed XML](https://stackoverflow.com/q/996552/3744182). – dbc Oct 25 '21 at 17:20
  • Also you can try [SgmlReader](https://www.nuget.org/packages/Microsoft.Xml.SgmlReader/) – Alexander Petrov Oct 25 '21 at 22:04

0 Answers0