XmlDocument problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ayende Rahien

    #1

    XmlDocument problem

    Serious problem

    I'm using Chris Lovett's SgmlReader class


    SgmlReader sr = new SgmlReader();
    XmlDocument xdoc = new XmlDocument();
    sr.DocType = "HTML";
    sr.InputStream = new System.IO.Strin gReader(node.In nerText);
    xdoc.Load(sr);
    foreach(XmlNode PotentiallyMali cous in xdoc.SelectNode s("//script |
    //embed //object | //frameset //frame //iframe | //meta | //link | //style |
    //@style"))
    {
    if (node.ParentNod e!=null)
    PotentiallyMali cous.ParentNode .RemoveChild(Po tentiallyMalico us);
    else
    xdoc.RemoveChil d(PotentiallyMa licous);
    }
    item.desc = xdoc.InnerText;


    Unfrotantely, I'm getting an exception on xdoc.Load(sr), saying:

    System.InvalidO perationExcepti on: The specified node cannot be
    inserted as the valid child of this node, because the specified node is the
    wrong type.
    at System.Xml.XmlD ocument.AppendC hildForLoad(Xml Node newChild,
    XmlDocument doc)
    at System.Xml.XmlL oader.LoadDocSe quence(XmlDocum ent parentDoc)
    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.RssFeed.Up dateItem(XmlNod e ItemNode, XmlNamespaceMan ager
    nsMgr, ArrayList& NewItems, Boolean Update) in c:\documents and
    settings\ayende \my documents\visua l studio projects\medar\ rssfeed.cs:line
    233
    at Roar.RssFeed.Up dateFeed(XmlDoc ument feed, ArrayList& NewItems,
    Boolean Update)

    I've no idea what is causing this.
    node.InnerText equals:
    <'a href="http://www.ncl.com/fleet/dawn/index.htm"><'im g
    src="http://monster2.script ing.com/z/images/archiveScriptin gCom/2003/12/01/d
    awn.jpg" width="125" height="59" border="0" align="right" hspace="15"
    vspace="5" alt="A picture named dawn.jpg"><'/a>Two articles, both from the
    NY Times, by coincidence happened to show up one after the other in my
    aggregator, a stark contrast of how two kinds of Americans live. The first
    <'a
    href="http://www.nytimes.com/2003/12/01/nyregion/01SHIP.html?ex= 1385614800&e
    n=f12a99f582744 ee2&ei=5007&par tner=USERLAND"> article<'/a> details the
    luxurious cruise ship Tom DeLay is bringing to the Republican National
    Convention in NYC in August, where George Bush will, presumably, be
    nominated for a second term as President. It's a very beautiful ship, very
    nice. The second <'a
    href="http://www.nytimes.com/2003/11/27/international/worldspecial/27LIST.ht
    ml?ex=138535560 0&en=1fa37d9cc6 c8ca0f&ei=5007& partner=USERLAN D">article<'/a>
    is the daily report of US soldiers killed in Iraq. Yesterday only one
    soldier died, David Goldberg, 20, an engineer in the Army reserve,
    based in Layton, Utah. Needless to say he won't be going to the Republican
    National Convention or riding on any cruise ships. "

    Any idea what could cause it? Or how to fix it?



  • Zürcher See

    #2
    Re: XmlDocument problem

    I use also the SgmlReader and I've never has some problem.

    I think your problem is that you need a "root" node, an XmlDocument must
    have a unique starting node like:

    <html>
    <head>
    ...
    </head>
    <body>
    ...
    </body>
    </html>

    or

    <doc>
    <chapter name=1>
    ...
    </chapter>
    <chapter name=2>
    ...
    </chapter>
    </doc>

    The stucture of your document is:
    <a ..><img .../> </a>
    <#text>
    <a ..></a>
    <#text>
    <a ..></a>
    <#text>

    If it's so try the following:

    new System.IO.Strin gReader("<p>"+n ode.InnerText+" </p>");



    "Ayende Rahien" <Ayende@no.spam > schrieb im Newsbeitrag
    news:%23QVjPhAu DHA.2380@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Serious problem
    >
    > I'm using Chris Lovett's SgmlReader class
    >
    >
    > SgmlReader sr = new SgmlReader();
    > XmlDocument xdoc = new XmlDocument();
    > sr.DocType = "HTML";
    > sr.InputStream = new System.IO.Strin gReader(node.In nerText);
    > xdoc.Load(sr);
    > foreach(XmlNode PotentiallyMali cous in xdoc.SelectNode s("//script |
    > //embed //object | //frameset //frame //iframe | //meta | //link | //style[/color]
    |[color=blue]
    > //@style"))
    > {
    > if (node.ParentNod e!=null)
    > PotentiallyMali cous.ParentNode .RemoveChild(Po tentiallyMalico us);
    > else
    > xdoc.RemoveChil d(PotentiallyMa licous);
    > }
    > item.desc = xdoc.InnerText;
    >
    >
    > Unfrotantely, I'm getting an exception on xdoc.Load(sr), saying:
    >
    > System.InvalidO perationExcepti on: The specified node cannot be
    > inserted as the valid child of this node, because the specified node is[/color]
    the[color=blue]
    > wrong type.
    > at System.Xml.XmlD ocument.AppendC hildForLoad(Xml Node newChild,
    > XmlDocument doc)
    > at System.Xml.XmlL oader.LoadDocSe quence(XmlDocum ent parentDoc)
    > 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.RssFeed.Up dateItem(XmlNod e ItemNode, XmlNamespaceMan ager
    > nsMgr, ArrayList& NewItems, Boolean Update) in c:\documents and
    > settings\ayende \my documents\visua l studio projects\medar\ rssfeed.cs:line
    > 233
    > at Roar.RssFeed.Up dateFeed(XmlDoc ument feed, ArrayList& NewItems,
    > Boolean Update)
    >
    > I've no idea what is causing this.
    > node.InnerText equals:
    > <'a href="http://www.ncl.com/fleet/dawn/index.htm"><'im g
    >[/color]
    src="http://monster2.script ing.com/z/images/archiveScriptin gCom/2003/12/01/d[color=blue]
    > awn.jpg" width="125" height="59" border="0" align="right" hspace="15"
    > vspace="5" alt="A picture named dawn.jpg"><'/a>Two articles, both from the
    > NY Times, by coincidence happened to show up one after the other in my
    > aggregator, a stark contrast of how two kinds of Americans live. The first
    > <'a
    >[/color]
    href="http://www.nytimes.com/2003/12/01/nyregion/01SHIP.html?ex= 1385614800&e[color=blue]
    > n=f12a99f582744 ee2&ei=5007&par tner=USERLAND"> article<'/a> details the
    > luxurious cruise ship Tom DeLay is bringing to the Republican National
    > Convention in NYC in August, where George Bush will, presumably, be
    > nominated for a second term as President. It's a very beautiful ship, very
    > nice. The second <'a
    >[/color]
    href="http://www.nytimes.com/2003/11/27/international/worldspecial/27LIST.ht[color=blue]
    >[/color]
    ml?ex=138535560 0&en=1fa37d9cc6 c8ca0f&ei=5007& partner=USERLAN D">article<'/a>[color=blue]
    > is the daily report of US soldiers killed in Iraq. Yesterday only one
    > soldier died, David Goldberg, 20, an engineer in the Army reserve,
    > based in Layton, Utah. Needless to say he won't be going to the Republican
    > National Convention or riding on any cruise ships. "
    >
    > Any idea what could cause it? Or how to fix it?
    >
    >
    >[/color]


    Comment

    • Ayende Rahien

      #3
      Re: XmlDocument problem


      "Zürcher See" <aquila@cannabi smail.com> wrote in message
      news:uV4wRlBuDH A.2304@TK2MSFTN GP12.phx.gbl...[color=blue]
      > I use also the SgmlReader and I've never has some problem.
      > Try
      > new System.IO.Strin gReader("<p>"+n ode.InnerText+" </p>");[/color]

      Dude!
      It works!
      Thanks a Lot


      Comment

      Working...