2010-09-06 78 views
0

我有以下代碼以從數據庫中的XML文檔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嗎?

EDIT

這裏是所生成的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之後)的一個例子,它仍然不能很好地格式化

回答

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); 
    } 
+0

它說「根元件丟失」錯誤! – Reda 2010-09-06 14:24:09

+0

這意味着您的XML不是具有單個根節點的格式良好的文檔。 – 2010-09-06 14:31:15

+0

周圍有解決方案嗎? – Reda 2010-09-07 06:16:32