Easiest way giving an XML file to a control

R

Ryan Ternier

I've never created controls before, but am experimenting with em. I'm
trying to create a listing control that displays a list of information, and
indents them accordingly to a formated XML file, using the nodes of the xml
file.

What's the easiest way of passing an XML file to a control? I don't want my
control to know where the XML came from, I just want it to load from an XML
file.

I was thinking of having one written, and the control loading it, but that
seems awfuly wasteful.

The list will display sort of the way Bullets work (with X amount of
indents). Each indent having it's own class / styles etc.

Any suggestions would be great!

Thanks!

Ryan Ternier
Code Monkey
 
S

Scott Mitchell [MVP]

Ryan, there are a number of ways you could tackle this problem. One
approach I use in my RssFeed control
[http://scottonwriting.net/sowBlog/RssFeed.htm] is to have a property
called DataSource that is of type object. It allows any of the
following to be assigned to it:

* A string path to an XML file
* A string path to the URL of an XML file
* An XmlReader object
* A TextReader object, or
* An XPathDocument object

The code for this (which can be found, as the control's complete source
can be downloaded) is:

public virtual object DataSource
{
get
{
return dataSource;
}
set
{
// make sure we're working with a string, XmlReader, or TextReader
if (value == null || value is string || value is XmlReader || value
is TextReader || value is XPathDocument)
dataSource = value;
else
throw new ArgumentException("DataSource must be assigned a string,
XmlReader, TextReader, or XPathDocument.");
}
}


Then, in my method where I actually work with the data I get the XML
data in the following way:

// Load the data into an XmlDocument object
XPathDocument xpDoc = null;
if (dataSource is XPathDocument)
xpDoc = (XPathDocument) dataSource;
else
{
if (dataSource is string)
{
...
}
else if (dataSource is TextReader)
xpDoc = new XPathDocument((TextReader) dataSource);
else if (dataSource is XmlReader)
xpDoc = new XPathDocument((XmlReader) dataSource);
}


hth!


Ryan said:
I've never created controls before, but am experimenting with em. I'm
trying to create a listing control that displays a list of information, and
indents them accordingly to a formated XML file, using the nodes of the xml
file.

What's the easiest way of passing an XML file to a control? I don't want my
control to know where the XML came from, I just want it to load from an XML
file.

I was thinking of having one written, and the control loading it, but that
seems awfuly wasteful.

The list will display sort of the way Bullets work (with X amount of
indents). Each indent having it's own class / styles etc.


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top