Solved

asp.net c# create dynamic xml from database

Posted on 2007-12-04
3
2,298 Views
Last Modified: 2013-12-16
Hi guys,

I wonder can you help,

Im working off of the following example that creates xml to be passed back to the client based on a string array with populated names.  

here is the coded example from the following book Im working off "Ajax for Web Application Developers"

    private void SearchUsers()
    {
        string request = Request["request"].ToString();

        string usernameXml = "";
        string[] usernames = new string[3] { "Kris Hadlock", "Grace Hopper", "Pi Sheng" };

        foreach (string username in usernames)
        {
            if (username.ToLower().Substring(0, request.Length) == request.ToLower())
            {
                usernameXml += "<username><![CDATA[" + username + "]]></username>";
            }
        }

        usernameXml = "<?xml version='1.0' encoding='iso-8859-1' ?><usernames>" + usernameXml + "</usernames>";
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(usernameXml);
        xDoc.Save(Response.OutputStream);
    }

I wish to achieve the same except I want to pull the usernames from the databse as part of a query and then parse them into the xml document.

Can someone show me how I could do this please?

Thanks very much!

Jimbo
0
Comment
Question by:jim_bob_jim
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 500 total points
ID: 20404925
Replace this code:
string[] usernames = new string[3] { "Kris Hadlock", "Grace Hopper", "Pi Sheng" };
 foreach (string username in usernames)
        {
            if (username.ToLower().Substring(0, request.Length) == request.ToLower())
            {
                usernameXml += "<username><![CDATA[" + username + "]]></username>";
            }
        }


with this:

OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["your connectionstring"]);
string strSQL = "SELECT username FROM usertable";
OleDbCommand objCmd = new OleDbCommand(strSQL, objConn);
objConn.Open();
OleDbDataReader readData = objCmd.ExecuteReader();
whilereadData.Read())
{
           usernameXml += "<username><![CDATA[" + readData["username"]+ "]]></username>";
}
readData.Close();
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 20404937
One correction:
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["Name_Of_Your_Connection_String"]);

You then need to define your connection in the AppSettings.  If using .NET 2 then you can use the actual ConnectionStrings section instead of AppSettings
0
 

Author Closing Comment

by:jim_bob_jim
ID: 31412633
Thank you very much.  I had got so close and your answer explained exactly what I was missing.

Much appreciated!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now