Solved

asp.net c# create dynamic xml from database

Posted on 2007-12-04
3
2,305 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
[X]
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
  • 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

630 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