Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Create XML file using database fields

Create XML file using database fields

Create XML file using database fields

Hi all,

I've never dealt with XML at all. I'm having to create an xml file based on values retrieved from sql query. I have pieced together the following mostly from googling for examples.  I'm getting an error when I try to open the file:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

A name was started with an invalid character. Error processing resource 'file:///C:/Mugshot.xml'. Line 1, Position 23

<?xml version="1.0"?><<BookingRecordIdentifier>ECSO11JBN013691</BookingRecordIdentifier>><<Per...

Anyone know how to resolve this issue?  It seems to me that it might be data related?

In addition, am I going about this the right way apart from the error?

Here's my code:

static void Main(string[] args)

            XmlTextWriter textWriter = new XmlTextWriter("c:\\Mugshot.xml", null);

            SqlConnection con = new SqlConnection("server=###;database=###;Initial Catalog=jail;uid=###;pwd=###");

            SqlCommand query = new SqlCommand("SELECT ltrim(rtrim(a.bookno)) as bookno,ltrim(rtrim(a.ecsoid)) as ecsoid, demdatatype = CASE when a.age > 17 then 'A' else 'J' END, ltrim(rtrim(a.race)) as race, ltrim(rtrim(a.height)) as height,ltrim(rtrim(a.lname)) as lname,ltrim(rtrim(a.fname)) as fname,ltrim(rtrim(a.sex)) as sex,ltrim(rtrim(a.dob)) as dob, ltrim(rtrim(b.fsn)) as fsn FROM jail.dbo.jailbook a, jail.dbo.jbcharge b where a.bookno = b.bookno and a.lname is not null");

            query.Connection = con;
            SqlDataReader reader;

            reader = query.ExecuteReader();

            while (reader.Read())

               // textWriter.WriteComment("<AddBookingRecord>");
                textWriter.WriteStartElement("<BookingRecordIdentifier>" + reader["bookno"] + "</BookingRecordIdentifier>");
                textWriter.WriteStartElement("<PersonIdentifier>" + reader["ecsoid"] + "</PersonIdentifier>");
                textWriter.WriteStartElement("<Demdatatype>" + reader["demdatatype"] + "</Demdatatype>");
                textWriter.WriteStartElement("<Race>" + reader["race"] + "</Race>");
                textWriter.WriteStartElement("<Height>" + reader["height"] + "</Height>");
                textWriter.WriteStartElement("<Lastname>" + reader["lname"] + "</Lastname>");
                textWriter.WriteStartElement("<Firstname>" + reader["fname"] + "</Firstname>");
                textWriter.WriteStartElement("<Gender>" + reader["sex"] + "</Gender>");
                textWriter.WriteStartElement("<Dob>" + reader["dob"] + "</Dob>");
                textWriter.WriteStartElement("<Charge>" + reader["fsn"] + "</Charge>");




Thanks so much for your help.

RE: Create XML file using database fields

Two points:

- You need a tag that encloses the whole document
- Looks like WriteStartElement adds < and > for you


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close