Click here to Skip to main content
15,898,996 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to add item to listbox control Pin
Saksida Bojan10-Aug-09 23:47
Saksida Bojan10-Aug-09 23:47 
GeneralRe: How to add item to listbox control Pin
DaveyM6910-Aug-09 23:50
professionalDaveyM6910-Aug-09 23:50 
Questionhtml in C# Pin
AndyInUK10-Aug-09 22:51
AndyInUK10-Aug-09 22:51 
AnswerRe: html in C# Pin
Eddy Vluggen10-Aug-09 23:12
professionalEddy Vluggen10-Aug-09 23:12 
GeneralRe: html in C# Pin
AndyInUK10-Aug-09 23:14
AndyInUK10-Aug-09 23:14 
GeneralRe: html in C# Pin
Eddy Vluggen10-Aug-09 23:38
professionalEddy Vluggen10-Aug-09 23:38 
GeneralRe: html in C# Pin
Pete O'Hanlon11-Aug-09 0:26
mvePete O'Hanlon11-Aug-09 0:26 
AnswerRe: html in C# [modified] Pin
0x3c010-Aug-09 23:32
0x3c010-Aug-09 23:32 
I'm probably overthinking this as usual, but for a service, what about a HTML generator class? It could have a list of HTMLTags, and each HTMLTag would have another list of HTMLTags for nested tags, an InnerText property and a Dictionary<string, string> for the attributes. Then when you wanted to generate the HTML, you would iterate through each HTMLTag and recursively write the string approximation of each HTMLTag. If you wanted to be really nifty, you could override the ToString function to make this easier

I'm feeling rather generous today, so I'll even give you "teh codz" for once. Something like this:
public class HTMLGenerator
{
	public List<HTMLTag> Tags { get; private set; }

	public HTMLGenerator(params HTMLTag[] tags)
	{
		this.Tags = new List<HTMLTag>(tags);
	}

	public string GetHTML()
	{
		StringBuilder sb = new StringBuilder();

		foreach(HTMLTag tag in this.Tags)
			sb.Append(tag.ToString());
		return sb.ToString();
	}
}

public class HTMLTag
{
	public List<HTMLTag> Subtags { get; private set; }

	public string InnerText { get; set; }

	public string Name { get; set; }

	public Dictionary<string, string> Attributes { get; private set; }

	public HTMLTag(string name, string innerText)
	{
		this.Name = name;
		this.InnerText = innertext;
		this.Subtags = new List<HTMLTag>();
		this.Attributes = new Dictionary<string, string>()
	}

	public override string ToString()
	{
		//The main meat of the code goes here
		StringBuilder sb = new StringBuilder();

		//Append the start and the name of the HTML tag
		sb.AppendFormat("<{0} ", this.Name);
		/* Then, iterate through each attribute in the Dictionary, appending it in string
		 * form. Note: you have to handle any special character encoding yourself */
		foreach(string attribute in this.Attributes)
			sb.AppendFormat("{0}=\"{1}\" ", attribute, this.Attributes[attribute]);
		if(this.Subtags.Count > 0)
		{
			//Close the tag and recurse for every child tag
			sb.AppendLine(">");
			foreach(HTMLTag tag in this.Subtags)
				sb.AppendLine(tag.ToString());
			sb.AppendFormat(">/{0}<", this.Name);
		}
		else
			/* If there aren't any subtags, we can just close it like this, without
			* writing an ending tag */
			sb.AppendLine(" />");
		return sb.ToString();
	}
}


[edit] Stretched the screen; fixed

Between the idea
And the reality
Between the motion
And the act
Falls the Shadow

modified on Tuesday, August 11, 2009 5:49 AM

QuestionHow to load a button in taskbar through c# Pin
svt gdwl10-Aug-09 22:34
svt gdwl10-Aug-09 22:34 
AnswerRe: How to load a button in taskbar through c# Pin
Arindam Sinha10-Aug-09 23:07
Arindam Sinha10-Aug-09 23:07 
Questioncallbackoncollected delegated data Pin
Rajee Maharjan10-Aug-09 22:28
Rajee Maharjan10-Aug-09 22:28 
QuestionValidation on Texboxes dialog in Setup and deployment project Pin
248912810-Aug-09 21:53
248912810-Aug-09 21:53 
AnswerRe: Validation on Texboxes dialog in Setup and deployment project Pin
Arindam Sinha10-Aug-09 22:58
Arindam Sinha10-Aug-09 22:58 
Questionhow ı can do "I delete old folder with hours but my prog delete days" Pin
mehmetbilge10-Aug-09 21:23
mehmetbilge10-Aug-09 21:23 
AnswerRe: how ı can do "I delete old folder with hours but my prog delete days" Pin
0x3c010-Aug-09 21:57
0x3c010-Aug-09 21:57 
Questiondata gridview editing Pin
abdolrab10-Aug-09 21:03
abdolrab10-Aug-09 21:03 
AnswerRe: data gridview editing Pin
padmanabhan N10-Aug-09 22:32
padmanabhan N10-Aug-09 22:32 
QuestionCombobox Control Pin
elci10-Aug-09 20:33
elci10-Aug-09 20:33 
AnswerRe: Combobox Control Pin
padmanabhan N10-Aug-09 22:33
padmanabhan N10-Aug-09 22:33 
QuestionProblem in using namespace in c# Pin
DIPAK@EMSYS10-Aug-09 20:16
DIPAK@EMSYS10-Aug-09 20:16 
AnswerRe: Problem in using namespace in c# Pin
SeMartens10-Aug-09 20:21
SeMartens10-Aug-09 20:21 
GeneralRe: Problem in using namespace in c# Pin
DIPAK@EMSYS10-Aug-09 21:34
DIPAK@EMSYS10-Aug-09 21:34 
GeneralRe: Problem in using namespace in c# Pin
Henry Minute11-Aug-09 0:29
Henry Minute11-Aug-09 0:29 
GeneralRe: Problem in using namespace in c# Pin
DIPAK@EMSYS11-Aug-09 17:57
DIPAK@EMSYS11-Aug-09 17:57 
QuestionDateTimePicker Pin
elci10-Aug-09 20:03
elci10-Aug-09 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.