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
<
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);
textWriter.WriteStartDocument();
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;
con.Open();
reader = query.ExecuteReader();
while (reader.Read())
{
textWriter.WriteComment("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteStartElement("
textWriter.WriteComment("
}
textWriter.WriteEndDocument();
textWriter.Close();
con.Close();
}
Thanks so much for your help.
Javeed M ShaikhPosted Oct 26, 2011, 9:08 AM
you have to pass a string to the method:
Replace
textWriter.WriteString(reader["bookno"]);
With
textWriter.WriteString(reader["bookno"].ToString());
Please do not forget to mark "Accepted Answer".
Javeed M ShaikhPosted Oct 26, 2011, 11:24 AM
Sam HobbsPosted Oct 26, 2011, 11:20 AM
Kevin FralickPosted Oct 26, 2011, 8:29 AM
"the best overloaded match for system.xml.xmlwriter.writestring has some invalid arguments"
On the bold line...
textWriter.WriteStartElement("bookingrecordidentifier");
textWriter.WriteString(reader["bookno"]);
textWriter.WriteEndElement();
Sam HobbsPosted Oct 25, 2011, 10:10 PM
You are certainly not using WriteStartElement correctly. So instead of:
I think you want:
textWriter.WriteString(reader["bookno"]);
textWriter.WriteEndElement();
I think you will have better results if you change that and the same for the other tags; PersonIdentifier and the others.
Javeed M ShaikhPosted Oct 25, 2011, 3:46 PM
DataTable dt = new DataTable();
dt.Load(reader);
dt.WriteXml(@"c:\Mugshot.xml");