C# HTML and XML HandlingThis C# section presents HTML and XML articles. It provides HtmlTextWriter and XElement examples.
Markup languages. HTML and XML are text-based markup languages that are commonly used to store data. They are easy to parse and share with other systems.
HTML is everywhere.
For this reason, being able to process it is important. The .NET Framework has the tools necessary for basic and advanced manipulation and creation of HTML.
Quote: HTML is the publishing language of the World Wide Web (World Wide Web Consortium).
There are many ways to handle HTML. In this introductory program, we use the HtmlTextWriter type, which is an abstract data type for generating HTML markup.HtmlTextWriter
Next: In this example we see that a span tag is opened and closed by the HtmlTextWriter.
C# program that writes HTML
static void Main()
using (StringWriter stringWriter = new StringWriter())
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
The term scraping means to download web pages and then scan the text and take parts into another application. You can use a C# program to scrape HTML links from web pages.Scrape HTML
Many different named colors are available in the hypertext markup language. These can also be specified directly inside CSS files. We print all the HTML colors.Color Table
XML stores structured data. The .NET Framework provides rich support for XML, including classes in System.Xml and System.Xml.Linq. XML does not require a specific implementation.
This program uses the System.Xml.Linq namespace to load XML files into XElement objects. There are other ways to handle XML but XElement is one of the easiest.XElement
Example XML file
C# program that loads example XML file
static void Main()
// Load the XML.
XElement element = XElement.Load("C:\\text.xml");
// Write text value.
To continue, we show how to write XML and also read in XML. XmlWriter and XmlReader are fast. You could use the types in parallel to implement persistence.XmlWriterXmlReader
XmlTextWriter: You can also find information about XmlTextWriter and XmlTextReader on this site.XmlTextWriterXmlTextReader
Quote: Extensible Markup Language, abbreviated XML, describes a class of data objects called XML documents.. (The World Wide Web Consortium).
In the real world, parsing HTML is fairly difficult because we must support badly formed markup. On the other hand, XML is easy to parse because it has strict rules for correctness.
Storing data. If you need to store data, using XML is often better than HTML. It is easier to handle in code. For optimal performance, binary data is a better option.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.