I want to use my WPF C# App code to export a database and save it on an XML file. Here's the code I'm using.
private void btnSave_Click(object sender, RoutedEventArgs e)
{
string FILENAME = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Connection.xml");
XDocument xdoc = XDocument.Load(FILENAME);
string conn = xdoc.Descendants("connectionStrings").FirstOrDefault().Value;
string query = "SELECT TOP 1000 [EMPID], [EMPName], [EMPRole], [EMPAddress], [EMPEmail], [EMPNumber] FROM [Login].[dbo].[tblEMP1] FOR XML PATH('ry'), ROOT('ty') ";
using (SqlConnection sqlCon = new SqlConnection(conn))
using (SqlCommand sqlCmd = new SqlCommand(query, sqlCon))
{
sqlCon.Open();
string result = sqlCmd.ExecuteScalar().ToString();
MessageBox.Show("XML Saved");
sqlCon.Close();
File.WriteAllText(@"C:\Users\user12\Desktop\test3.xml", result);
}
}
The problem is it does not display the xml tags as I would like for example <ty>,<ry>,<EMPID>,<EMPName> etc. do not appear. The XML runs fine on Microsoft SQL Server Management Studio. This is what the XML looks like when I run it on Microsoft SQL Server Management Studio.
<ty>
<ry>
<EMPID>1</EMPID>
<EMPName>Thabo</EMPName>
<EMPRole>Developer </EMPRole>
<EMPAddress>227 Complex B WoodMans Road Claremont</EMPAddress>
<EMPEmail>[email protected]</EMPEmail>
<EMPNumber>083 577 8910</EMPNumber>
</ry>
<ry>
<EMPID>2</EMPID>
<EMPName>Aldrin</EMPName>
<EMPRole>Analyst </EMPRole>
<EMPAddress>65 Mfecane Avenue</EMPAddress>
<EMPEmail>[email protected]</EMPEmail>
<EMPNumber>0872343352</EMPNumber>
</ry>
<ry>
<EMPID>4</EMPID>
<EMPName>Amoleng</EMPName>
<EMPRole>Engineer </EMPRole>
<EMPAddress>43 Pixely KaSeme Street</EMPAddress>
<EMPEmail>[email protected]</EMPEmail>
<EMPNumber>0765546832</EMPNumber>
</ry>
</ty>
But when I save it on using my app on visual studio. This is what I get as a result(when opened with a browser as an XML):
1ThaboDeveloper 227 Complex B WoodMans Road [email protected] 577 89102AldrinAnalyst 65 Mfecane [email protected] 43 Pixely KaSeme [email protected]
As you can see everything is clustered with no tags
This is what happens When I open it with notepad;
<ty><ry><EMPID>1</EMPID><EMPName>Thabo</EMPName><EMPRole>Developer </EMPRole><EMPAddress>227 Complex B WoodMans Road Claremont</EMPAddress><EMPEmail>[email protected]</EMPEmail><EMPNumber>083 577 8910</EMPNumber></ry><ry><EMPID>2</EMPID><EMPName>Aldrin</EMPName><EMPRole>Analyst </EMPRole><EMPAddress>65 Mfecane Avenue</EMPAddress><EMPEmail>[email protected]</EMPEmail><EMPNumber>0872343352</EMPNumber></ry><ry><EMPID>4</EMPID><EMPName>Amoleng</EMPName><EMPRole>Engineer </EMPRole><EMPAddress>43 Pixely KaSeme Street</EMPAddress><EMPEmail>[email protected]</EMPEmail><EMPNumber>0765546832</EMPNumber></ry>
As you can see the tags appear now but its still clustered.
My goal is to make a ready to use XML that won't require additional manipulating and editing and I want the tags to appear all the time with proper layout as it does in SQL Server Management Studio example.
Thanks in advance.
