SgmlReader problem...

  • Thread starter Thread starter Ayende Rahien
  • Start date Start date
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;
}
 
Without the code for SgmlReader...it's anyone's guess what the problem is.
My guess is you are passing a null stream to this method
System.IO.StreamReader..ctor because the uri passed to
Sgml.SgmlReader.LazyLoadDtd(Uri baseUri) is invalid or null or cannot be
resolved

Colin
 
I found the problem, apperantly I compiled without embedding a resource,
which caused SgmlReader to output this message.

Colin Savage said:
Without the code for SgmlReader...it's anyone's guess what the problem is.
My guess is you are passing a null stream to this method
System.IO.StreamReader..ctor because the uri passed to
Sgml.SgmlReader.LazyLoadDtd(Uri baseUri) is invalid or null or cannot be
resolved

Colin

Ayende Rahien said:
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;
}
 
Back
Top