Click here to Skip to main content
15,391,989 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to serialize HtmlElement object in file stream??
Posted

use this code to serialize any object into a file stream ... in your case replace the Object type into HtmlElement

Object obj=new Object();
     XmlSerializer sw = new XmlSerializer(typeof(Object));
     FileStream fs = new FileStream("**Full FilePath**", FileMode.OpenOrCreate);
     sw.Serialize(fs, obj);
     fs.Close();


hope this helped,good luck.
   
Comments
yogeshcgs 11-Aug-10 11:33am
   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
HtmlElement obj;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

XmlSerializer sw = new XmlSerializer(typeof(HtmlElement ));
FileStream fs = new FileStream("1.txt", FileMode.OpenOrCreate);
sw.Serialize(fs, obj);
fs.Close();
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement htmlel in col)
{
if (htmlel != null)
{
obj = htmlel;
return;
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("www.digg.com");
}
}
}


please see the code.

i am getting error....
System.Windows.Forms.HtmlElement cannot be serialized because it does not have a parameterless constructor.
yogeshcgs 11-Aug-10 11:34am
   
please help me out. its very urgent.........
thanks a lot, i will check it out.
   
hmmmmm ... yeah XML serialization is not gonna work for you if the class does not have parameterless constructor :sigh: one way to go around that is to create a wrapper class for the HTMLElement class like the code below and serialize the wrapper class
public class HtmlElementWrapper
       {
           public HtmlElementWrapper()
           { }

           public HtmlElementWrapper(HtmlElement htmlElement)
           {
               this.HtmlElement = htmlElement;
           }

           public HtmlElement HtmlElement
           { get; set; }
       }

or you can use Binary Serialization check out this article for that

Object Serialization using C#

good luck
   
Comments
yogeshcgs 11-Aug-10 14:11pm
   
it did not work error.......
yogeshcgs 11-Aug-10 23:47pm
   
i tried this, but it does not work..............
Samuel Cherinet 12-Aug-10 9:19am
   
poooh ... don't know at this point tried it on binary serialization as well and unfortunately HTMLElement is not marked as serializable. at this point the only thing I can ask you is "what is the need for you to serialize an Xml element?? " I mean you can use the HtmlElement.OuterHtml to get the element as plain string and save it, because that is what XMLSerialization basically is going to do.

other than that I hope one of those fine .net developers in here would resolve this issue for you"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900