我有以下代码来显示XML文档中数据库的数据
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;
}
生成的XML文件全部显示在一行中。有人可以建议编辑以正常的多行格式制作XML吗?
修改
以下是生成的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>
它曾经在一行中显示但是现在(使用XDocument之后)它仍然没有很好地格式化
答案 0 :(得分:3)
如果问题是源XML字符串不包含必要的格式,您可以将XML加载到XmlDocument中,然后使用它来写入带有格式的流。
这是一个简单的例子。
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlParam.Value.ToString());
using (StreamWriter wIn = new StreamWriter(xmlFile, false))
using (XmlTextWriter wr = new XmlTextWriter(wIn))
{
wr.Formatting = Formatting.Indented;
doc.WriteTo(wr);
}