Parse XML

  • Thread starter Thread starter Sami
  • Start date Start date
S

Sami

Im new to c#
what's the easiest way to parse short xml files?

<html><head><title>Title of the
page</title><script></script></head><body>BODY</body></html>

and save it to an object for access later?

Class HtmlFile
{
public string Title
public string SciptType
public string SciptSrc
public string Body
}

XmlNodeReader seems very confusing

Sami
 
Im new to c#
what's the easiest way to parse short xml files?

<html><head><title>Title of the
page</title><script></script></head><body>BODY</body></html>

and save it to an object for access later?

Class HtmlFile
{
public string Title
public string SciptType
public string SciptSrc
public string Body

}

XmlNodeReader seems very confusing

The two basic methods of loading an XML file from my experiance is 1)
use the DOM (Document Object Model) which uses the XmlDocument object
to load the file, and the SelectSingleNode method to get the value of
a single node, and 2) use the XmlSerializer class.
 
if it's not too much trouble could someone provide a sample on how to do
this?

thank you
Sami
Im new to c#
what's the easiest way to parse short xml files?

<html><head><title>Title of the
page</title><script></script></head><body>BODY</body></html>

and save it to an object for access later?

Class HtmlFile
{
public string Title
public string SciptType
public string SciptSrc
public string Body

}

XmlNodeReader seems very confusing

The two basic methods of loading an XML file from my experiance is 1)
use the DOM (Document Object Model) which uses the XmlDocument object
to load the file, and the SelectSingleNode method to get the value of
a single node, and 2) use the XmlSerializer class.
 
if it's not too much trouble could someone provide a sample on how to do
this?

Using Linq to Xml:

static void Main(string[] args) {
string xml = "<html><head><title>Title of the page</
title><script></script></head><body>BODY</body></html>";
HtmlFile hf = new HtmlFile(xml);
}

class HtmlFile {
public string Title;
public string Script;
public string Body;
// ...

public HtmlFile(string xml) {
XElement e = XElement.Parse(xml);

if (e.Element("head") != null) {
Title =
(string)e.Element("head").Element("title");
Script =
(string)e.Element("head").Element("script");
}
Body = (string)e.Element("body");
// ...
}
 
Back
Top