I have the following Code to display Data from the Database in XML Document
public void generate_XML_AllTables(string Dir)
{
SqlDataReader Load_SP_List = null; //SQL reader that gets list of stored procedures in the database
SqlDataReader DataclassId = null; //SQL reader to get the DataclassIds from tables
SqlConnection conn = null;
conn = new SqlConnection("Data Source= --SOME DATABASE--; persist security info=False;Trusted_Connection=Yes");
SqlConnection conn_2 = null;
conn_2 = new SqlConnection("Data Source= --SOME DATABASE--; persist security info=False;Trusted_Connection=Yes");
SqlCommand getDataclassId_FromTables;
int num_SP = 0, num_Tables = 0;
string strDataClass; //Name of table
string sql_str; //SQL command to get
conn.Open();
//Selecting all Load Stored Procedures of CLNT & Get the table names
// to pass the Load operation which generates the XML docs.
SqlCommand cmd = new SqlCommand("Select * from sys.all_objects where type_desc='SQL_STORED_PROCEDURE' and name like 'CLNT%Load';", conn);
Load_SP_List = cmd.ExecuteReader();
while (Load_SP_List.Read())
{
//Gets the list of Stored Procedures, then modifies it
//to get the table names
strDataClass = Load_SP_List[0].ToString();
strDataClass = strDataClass.Replace("CLNT_", "");
strDataClass = strDataClass.Replace("_Load", "");
sql_str = "select DataclassId from " + strDataClass;
//Gets the DataclassID's from the tables then passes
//the parameters to the method Run_Load_StoredProcedure
//(Table name, DataclassID)
conn_2.Open();
getDataclassId_FromTables = new SqlCommand(sql_str, conn_2);
DataclassId = getDataclassId_FromTables.ExecuteReader();
while (DataclassId.Read())
{
string test = DataclassId[0].ToString();
Guid oRootGuid = new Guid(test);
run_Load_StoredProcedure(strDataClass, oRootGuid, Dir);
num_Tables++;
}
DataclassId.Close();
conn_2.Close();
num_SP++;
}
Load_SP_List.Close();
conn.Close();
System.Console.WriteLine("{0} of Stored Procedures have been executed and {1} of XML Files have been generated successfully..", num_SP,num_Tables);
}
public string run_Load_StoredProcedure(string strDataClass, Guid guidRootId, string Dir)
{
SqlDataReader rdr = null;
SqlConnection conn = null;
conn = new SqlConnection("Data Source= --SOME DATABASE--; persist security info=False;Trusted_Connection=Yes");
conn.Open();
// Procedure call with parameters
SqlCommand cmd = new SqlCommand("CLNT_" + strDataClass + "_Load", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
//Adding parameters, in- and output
SqlParameter idParam = new SqlParameter("@DataclassId", SqlDbType.UniqueIdentifier);
idParam.Direction = ParameterDirection.Input;
idParam.Value = guidRootId;
SqlParameter xmlParam = new SqlParameter("@XML", SqlDbType.VarChar, -1 /*MAX*/ );
xmlParam.Direction = ParameterDirection.Output;
cmd.Parameters.Add(idParam);
cmd.Parameters.Add(xmlParam);
rdr = cmd.ExecuteReader(CommandBehavior.SingleResult);
DirectoryInfo dest = new DirectoryInfo(Dir + "\\Backup");
DirectoryInfo source = new DirectoryInfo(Dir);
if (source.Exists == false)
{
source.Create();
if (dest.Exists == false)
{
dest.Create();
}
}
string xmlFile = @Dir + "\\" + strDataClass + " [" + guidRootId + "].xml";
//The value of the output parameter ‘xmlParam’ will be saved in XML format using the StreamWriter.
System.IO.StreamWriter wIn = new System.IO.StreamWriter(xmlFile, false);
wIn.WriteLine(xmlParam.Value.ToString());
wIn.Close();
rdr.Close();
conn.Close();
return xmlFile;
}
The problem the generated XML Files are all displayed in One Line. Can someone suggest an edit to make the XMLs in a normal Multi-line format?
EDIT
Here is an example of the generated XML
<CT_MilitaryUsers Version="1" DataSource="Source" ModDttm="2010-07-20T14:13:55.320" ModUser="EUADEV\A003893" ModuleOwner="EUADEVS06\SS2008" CreateDttm="2010-07-20T14:13:55.320" CreateUser="EUADEV\A003893">
<CtMilitaryUsers DataclassId="8BA475CB-5582-481B-A3DE-099F4E59D323" EntityId="8BA475CB-5582-481B-A3DE-099F4E59D323" Name="CTP" IsExtMilUser="0" />
</CT_MilitaryUsers><CT_MilitaryUsers Version="1" DataSource="Source" ModDttm="2010-07- 20T14:13:55.320" ModUser="EUADEV\A003893" ModuleOwner="EUADEVS06\SS2008" CreateDttm="2010-07-20T14:13:55.320" CreateUser="EUADEV\A003893"><CtMilitaryUsers DataclassId="8BA475CB-5582-481B-A3DE-099F4E59D323" EntityId="8BA475CB-5582-481B-A3DE-099F4E59D323" Name="CTP" IsExtMilUser="0"/></CT_MilitaryUsers>
it used to be displayed in one line but even now (after using the XDocument) it's still not well formatted