C# XML显示在一行中

C# XML显示在一行中,c#,xml,C#,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

我有以下代码以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后),它的格式仍然不好

如果问题是源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); 
  }

如果问题是源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); 
  }

它说“根元素丢失”错误!!这意味着您的XML不是一个具有单个根节点的格式良好的文档。有解决方案吗?@Reda,我没有看到有问题的XML,但简而言之,唯一的选择是更正XML。如果问题是XML实际上是多个XML片段,那么您需要使用XmlDocument单独格式化每个片段,或者可以在所有片段周围放置一个根元素,然后格式化所有内容并随后提取片段。但是我需要看到有问题的XML才能更有用。它们很多,我在我的原始帖子中添加了一个。它说“根元素丢失”错误!!这意味着您的XML不是一个具有单个根节点的格式良好的文档。有解决方案吗?@Reda,我没有看到有问题的XML,但简而言之,唯一的选择是更正XML。如果问题是XML实际上是多个XML片段,那么您需要使用XmlDocument单独格式化每个片段,或者可以在所有片段周围放置一个根元素,然后格式化所有内容并随后提取片段。但我需要看到令人不快的XML,这样才能更有帮助。它们很多,我在原来的帖子中添加了一个。