...">

.NET XML uselessly resolves objects in Save

I have a simple XML file:

<?xml version="1.0" encoding="UTF-8"?>
<foo attr="blah &#176; blah"/>

When I load it into .NET XmlDocument and print Save, ie:

xmlDoc = New XmlDocument()
xmlDoc.Load("c:\temp\bar.xml")
xmlDoc.Save("c:\temp\bad.xml")

the new XML file contains the permitted amplifier 176 (degree sign). This then breaks the final black box that I am trying to load into XML.

I tried to play with encoding, which is ineffective. Is it possible for the parser to simply respond to what happened without resolving the entity? Inert, it does not allow & # 176;

+3
source share
1 answer

XmlDocument Load frees characters, also plays with it and cannot find any simple solution on how to stop this behavior.

-

foreach (XmlNode xn in xdoc.SelectNodes("descendant-or-self::*"))
{
  foreach(XmlAttribute attr in xn.Attributes)
  {
    string val = System.Web.HttpUtility.HtmlEncode(attr.Value);
    attr.Value = val;
  }
  if (!xn.InnerXml.Contains("<"))
  {
    string val = System.Web.HttpUtility.HtmlEncode(xn.InnerText);
    xn.InnerText = val;
  }
}

.Save(); , , .

+1

Source: https://habr.com/ru/post/1758708/


All Articles