Easiest way giving an XML file to a control

Discussion in 'ASP .Net Web Controls' started by Ryan Ternier, Nov 15, 2004.

  1. Ryan Ternier

    Ryan Ternier Guest

    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
    Ryan Ternier, Nov 15, 2004
    #1
    1. Advertising

  2. 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 Ternier wrote:
    > 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

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Nov 15, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Sharman
    Replies:
    4
    Views:
    5,174
    Nikolai Chuvakhin
    Jul 24, 2003
  2. sixteenmillion

    The giving that keeps on giving

    sixteenmillion, Nov 19, 2007, in forum: C Programming
    Replies:
    0
    Views:
    412
    sixteenmillion
    Nov 19, 2007
  3. gnewsgroup
    Replies:
    5
    Views:
    444
    Ismail
    Jan 3, 2008
  4. JJ
    Replies:
    1
    Views:
    444
    Munna
    Jun 16, 2008
  5. Raymond Schanks
    Replies:
    0
    Views:
    325
    Raymond Schanks
    Jul 15, 2010
Loading...

Share This Page