A
Ayende Rahien
I've a mysterious problem using SgmlReader, I get an exception doing to
following code:
I get the following exception calling CleanHtml("Painless software
management");
"System.ArgumentNullException: Value cannot be null.
Parameter name: stream
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean
detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(Stream stream)
at Sgml.SgmlReader.LazyLoadDtd(Uri baseUri)
at Sgml.SgmlReader.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean
preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at Roar.RoarCache.CleanHtml(String Html) in f:\vs.net
projects\roar\roarcache.cs:line 528"
private static string CleanHtml(string Html)
{
Sgml.SgmlReader sr = new Sgml.SgmlReader();
XmlDocument xdoc = new XmlDocument();
sr.DocType = "HTML";
sr.InputStream = new System.IO.StringReader("<p>"+Html+"</p>");
xdoc.Load(sr);
foreach(XmlNode PotentiallyMalicous in
xdoc.SelectNodes("//@[local-name()='style'] |
//@*[starts-with(local-name(),'on')] | //*[local-name()='script' or
local-name()='object'or local-name()='embed' or local-name()='iframe' or
local-name()='meta' or local-name()='frame'or local-name()='frameset' or
local-name()='link' or local-name()='style']"))
{
if(PotentiallyMalicous.NodeType == XmlNodeType.Attribute)
{
XmlAttribute PotentiallyMalicousAttribute = PotentiallyMalicous as
XmlAttribute;
PotentiallyMalicousAttribute.OwnerElement.Attributes.Remove(PotentiallyMalic
ousAttribute);
}
else
//No need to check for PotentiallyMalicous.ParentNode == null because I
added the <p></p> when composing it
PotentiallyMalicous.ParentNode.RemoveChild(PotentiallyMalicous);
}
return xdoc.InnerXml;
}
following code:
I get the following exception calling CleanHtml("Painless software
management");
"System.ArgumentNullException: Value cannot be null.
Parameter name: stream
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean
detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(Stream stream)
at Sgml.SgmlReader.LazyLoadDtd(Uri baseUri)
at Sgml.SgmlReader.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean
preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at Roar.RoarCache.CleanHtml(String Html) in f:\vs.net
projects\roar\roarcache.cs:line 528"
private static string CleanHtml(string Html)
{
Sgml.SgmlReader sr = new Sgml.SgmlReader();
XmlDocument xdoc = new XmlDocument();
sr.DocType = "HTML";
sr.InputStream = new System.IO.StringReader("<p>"+Html+"</p>");
xdoc.Load(sr);
foreach(XmlNode PotentiallyMalicous in
xdoc.SelectNodes("//@[local-name()='style'] |
//@*[starts-with(local-name(),'on')] | //*[local-name()='script' or
local-name()='object'or local-name()='embed' or local-name()='iframe' or
local-name()='meta' or local-name()='frame'or local-name()='frameset' or
local-name()='link' or local-name()='style']"))
{
if(PotentiallyMalicous.NodeType == XmlNodeType.Attribute)
{
XmlAttribute PotentiallyMalicousAttribute = PotentiallyMalicous as
XmlAttribute;
PotentiallyMalicousAttribute.OwnerElement.Attributes.Remove(PotentiallyMalic
ousAttribute);
}
else
//No need to check for PotentiallyMalicous.ParentNode == null because I
added the <p></p> when composing it
PotentiallyMalicous.ParentNode.RemoveChild(PotentiallyMalicous);
}
return xdoc.InnerXml;
}