I've a mysterious problem using SgmlReader, I get an exception doing to
following code:
I get the following exception calling CleanHtml("Pain less software
management");
"System.Argumen tNullException: Value cannot be null.
Parameter name: stream
at System.IO.Strea mReader..ctor(S tream stream, Encoding encoding, Boolean
detectEncodingF romByteOrderMar ks, Int32 bufferSize)
at System.IO.Strea mReader..ctor(S tream stream)
at Sgml.SgmlReader .LazyLoadDtd(Ur i baseUri)
at Sgml.SgmlReader .Read()
at System.Xml.XmlL oader.Load(XmlD ocument doc, XmlReader reader, Boolean
preserveWhitesp ace)
at System.Xml.XmlD ocument.Load(Xm lReader reader)
at Roar.RoarCache. CleanHtml(Strin g Html) in f:\vs.net
projects\roar\r oarcache.cs:lin e 528"
private static string CleanHtml(strin g Html)
{
Sgml.SgmlReader sr = new Sgml.SgmlReader ();
XmlDocument xdoc = new XmlDocument();
sr.DocType = "HTML";
sr.InputStream = new System.IO.Strin gReader("<p>"+H tml+"</p>");
xdoc.Load(sr);
foreach(XmlNode PotentiallyMali cous in
xdoc.SelectNode s("//@[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'o r local-name()='framese t' or
local-name()='link' or local-name()='style']"))
{
if(PotentiallyM alicous.NodeTyp e == XmlNodeType.Att ribute)
{
XmlAttribute PotentiallyMali cousAttribute = PotentiallyMali cous as
XmlAttribute;
PotentiallyMali cousAttribute.O wnerElement.Att ributes.Remove( PotentiallyMali c
ousAttribute);
}
else
//No need to check for PotentiallyMali cous.ParentNode == null because I
added the <p></p> when composing it
PotentiallyMali cous.ParentNode .RemoveChild(Po tentiallyMalico us);
}
return xdoc.InnerXml;
}
following code:
I get the following exception calling CleanHtml("Pain less software
management");
"System.Argumen tNullException: Value cannot be null.
Parameter name: stream
at System.IO.Strea mReader..ctor(S tream stream, Encoding encoding, Boolean
detectEncodingF romByteOrderMar ks, Int32 bufferSize)
at System.IO.Strea mReader..ctor(S tream stream)
at Sgml.SgmlReader .LazyLoadDtd(Ur i baseUri)
at Sgml.SgmlReader .Read()
at System.Xml.XmlL oader.Load(XmlD ocument doc, XmlReader reader, Boolean
preserveWhitesp ace)
at System.Xml.XmlD ocument.Load(Xm lReader reader)
at Roar.RoarCache. CleanHtml(Strin g Html) in f:\vs.net
projects\roar\r oarcache.cs:lin e 528"
private static string CleanHtml(strin g Html)
{
Sgml.SgmlReader sr = new Sgml.SgmlReader ();
XmlDocument xdoc = new XmlDocument();
sr.DocType = "HTML";
sr.InputStream = new System.IO.Strin gReader("<p>"+H tml+"</p>");
xdoc.Load(sr);
foreach(XmlNode PotentiallyMali cous in
xdoc.SelectNode s("//@[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'o r local-name()='framese t' or
local-name()='link' or local-name()='style']"))
{
if(PotentiallyM alicous.NodeTyp e == XmlNodeType.Att ribute)
{
XmlAttribute PotentiallyMali cousAttribute = PotentiallyMali cous as
XmlAttribute;
PotentiallyMali cousAttribute.O wnerElement.Att ributes.Remove( PotentiallyMali c
ousAttribute);
}
else
//No need to check for PotentiallyMali cous.ParentNode == null because I
added the <p></p> when composing it
PotentiallyMali cous.ParentNode .RemoveChild(Po tentiallyMalico us);
}
return xdoc.InnerXml;
}
Comment