Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLtextreader

Status
Not open for further replies.

j4606

MIS
Nov 28, 2005
349
US
I'm relatively new to c#. Been given the job of remaking a public site our company hosts. The site will have many visitors, and some of it's content needs to be xml driven.
So far I've been able get diferent tests working, but there are so many methods I don't exactly see which one is best. Thats mainly my question. For a public xml driven site whats the best method for reading an xml file into the contents of the site. I've researched examples using xmldocument, xmltextreaders, datagrids ect.. So far I like the xmlTextReader method but I'm not to sure its the best method. Below is some sample code I'm using for one part of the site. I return the string to an aspx page.
Code:
private string readXML() {
        string message = "";
        XmlTextReader xmlReader = null;
        xmlReader = new XmlTextReader(Server.MapPath("careers.xml"));
        xmlReader.WhitespaceHandling = WhitespaceHandling.None;
        while (xmlReader.Read())
        {
            switch (xmlReader.Name.ToString())
            {
                case "careers":
                    if (xmlReader.IsStartElement())
                    {
                        message +="<div id='mainContainer'>";
                    }
                    else {
                        message +="</div>";
                    }
                break;
                case "job":
                    if (xmlReader.IsStartElement())
                    {
                        xmlReader.MoveToAttribute(0);
                        message += "<span class='title'>" + xmlReader.Value + "</span><br />";
                    }
                break;
                case "description":
                    if (xmlReader.IsStartElement())
                    {
                        message += "<p class='jobDesc'>" + xmlReader.ReadString() + "</p>";
                    }
                break;
                case "list":
                    if (xmlReader.IsStartElement())
                    {
                        message += "<ul>";
                        xmlReader.MoveToAttribute(0);
                        message += xmlReader.Value;
                    }
                    else {
                        message += "</ul>";
                    }
                break;
                case "li":
                    if (xmlReader.IsStartElement())
                    {
                        message += "<li>" + xmlReader.ReadString() + "</li>";
                    }
                break;
                default:
                break;
            }
        }
        return message;
    }
Any help would be appreciated, thanks in advance.
 
xmldocument is considered much slower than xmlreader. i think this is due to xmldocument having navigation features within the document and xmlreader is a forward only reader.

but chrissie is right, if your transforming xml into html, use xslt. also .net works to reduce speggitte(sp) code of mixing html and c# which is what your message += "<div>.."; code is above.

if you must build out a string in .net use System.Text.StringBuilder().

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top