Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

asp.net c# create dynamic xml from database

Posted on 2007-12-04
3
2,301 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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