Solved

asp.net c# create dynamic xml from database

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server XML Select sub tables 4 64
SSIS Column mapping 5 40
Datagridview column resizing 8 27
How do i delete the last node in an xml in T-SQL 7 26
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.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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