views:

87

answers:

2

how do you create a class to read the html body and phase it into a variable?

Example the page http://domain.com/page1.aspx

display the following plaintext within the html body content

item1=xyz&item2=abc&item3=jkl

how do you read content of the html body and assign them to a variables

in this case

variable1=xyz (value taken from item1=)

variable2=abc (value taken from item2=)

variable3=jkl (value taken from item3=)?

+1  A: 

I think you mean query string but not html body. In that case you can use ASP.NET Page class's property Context as follows

string var1 = Context.Request.QueryString["item1"];
ILya
K001
ILya
+1  A: 

It is a two step process.

First you need to get body contents.

Second you need to parse content and assign to variables.

Getting body content code look like this:

Regex exp = new Regex(@"((?:.(?!<body[^>]*>))+.<body[^>]*>)|(</body\>.+)", RegexOptions.IgnoreCase);
string InputText = content;

string[] MatchList = exp.Split(InputText);
string body = MatchList[2];

Parsing code looks like:

        string body = content;
        string [] param = {"&"};
        string[] anotherParam = { "=" };
        string[] str = body.Split(param , StringSplitOptions.RemoveEmptyEntries);
        System.Collections.Hashtable table = new System.Collections.Hashtable();
        foreach (string item in table)
        {
            string[] arr = item.ToString().Split(anotherParam, StringSplitOptions.RemoveEmptyEntries);
            if(arr.length != 2)
                 continue;
            if(!table.Contains(arr[0])){
                table.Add(arr[0], arr[1]);
            }                
        }
Adeel
-1 for ignoring exceptions
John Saunders
@john thanks fixed it.
Adeel
@Adeel: no you didn't! Your code is now equivalent to not having the try/catch block at . So, get rid of the try/catch block. What are you trying to achieve? Why do you think you need a try/catch block at all?
John Saunders
Adeel
šljaker
@šljaker In the code, we are extracting values from page.
Adeel
@Adeel: what if the exception being thrown was because of some other problem? You'd be hiding that other problem. If you're concerned about duplicate keys, then check before adding.
John Saunders