简体   繁体   English

数据集到xml空值

[英]Dataset to xml Null Values

I have the code below, where from 3 tables I take the data and write an xml. 我有下面的代码,从3个表中获取数据并编写xml。
I want write (when a record column has null value) the column on the xml with null value. 我想写(当记录列具有null值时)XML上具有null值的列。 For example if (Category_name == Null ) to write on the xml (Null) Right now the code skip the column and don't even have this column on the xml. 例如,如果(Category_name == Null)在xml上写(Null)现在,代码跳过该列,甚至在xml上都没有此列。

 string xmlFileData = "";

    string[] tables = new string[] { "category",  "company", "config" };
    string query;
    xmlFileData += "<MyXml>";
    SqlConnection conn;
    dbconnect obj;
    obj = new dbconnect();//initailizing class object
    for (int i = 0; i < tables.Length; i++)
    {
        string ifemptquery;
        DataSet ds = new DataSet();

        DataSet ds1 = new DataSet();
        conn = obj.getConnection(); //calling connection function

        ifemptquery = "SELECT * FROM " + tables[i] ";
        SqlCommand cmd1 = new SqlCommand(ifemptquery, conn);
        conn.Open();
        SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
        conn.Close();
        if (dt1.Rows.Count > 0)
        {
            query = "SELECT * FROM " + tables[i] ";
            SqlCommand cmd = new SqlCommand(query, conn);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            conn.Close();
            conn.Dispose();
            ds.DataSetName = tables[i];
            string vartbname = tables[i];
            string trimed_tbname = vartbname.Replace("_", "");
            ds.Tables[0].TableName = trimed_tbname;
            xmlFileData += ds.GetXml();
        }
        else
        {

        }


    }
    xmlFileData += "</MyXml>";
    File.WriteAllText(Server.MapPath("~/xmlbackup/") + "Backup.xml", xmlFileData);

Refer similar question here - dataSet.GetXml() doesn't return xml for null or blank columns 在这里引用类似的问题-dataSet.GetXml()不为空列或空白列返回xml

Apart from solutions mentioned there, you can also traverse through dataset and write XML using XmlTextWriter. 除了此处提到的解决方案之外,您还可以遍历数据集并使用XmlTextWriter编写XML。 This method is not recommended if you are dealing with huge data. 如果您要处理大量数据,则不建议使用此方法。

I have been searching the whole world for a solution of writing null fields to XML using DataSet.WriteXML(). 我一直在全世界范围内寻找使用DataSet.WriteXML()将空字段写入XML的解决方案。 The answer posted by Vlad is the one I also used in my project but I found that following works in a much more performance optimized way. Vlad发布的答案是我在项目中也使用过的答案,但是我发现后续的工作以更加优化性能的方式进行。 I have created a function for your convenience. 为了方便起见,我创建了一个函数。 Change your dataset tables one after the other by calling the following function and replacing the tables. 通过调用以下函数并替换表,一个接一个地更改数据集表。

private DataTable GetNullFilledDataTableForXML(DataTable dtSource)
{
    // Create a target table with same structure as source and fields as strings
    // We can change the column datatype as long as there is no data loaded
    DataTable dtTarget = dtSource.Clone();
    foreach (DataColumn col in dtTarget.Columns)
        col.DataType = typeof(string);

    // Start importing the source into target by ItemArray copying which 
    // is found to be reasonably fast for nulk operations. VS 2015 is reporting
    // 500-525 milliseconds for loading 100,000 records x 10 columns 
    // after null conversion in every cell which may be usable in many
    // circumstances.
    // Machine config: i5 2nd Gen, 8 GB RAM, Windows 7 64bit, VS 2015 Update 1
    int colCountInTarget = dtTarget.Columns.Count;
    foreach (DataRow sourceRow in dtSource.Rows)
    {
        // Get a new row loaded with data from source row
        DataRow targetRow = dtTarget.NewRow();
        targetRow.ItemArray = sourceRow.ItemArray;

        // Update DBNull.Values to empty string in the new (target) row
        // We can safely assign empty string since the target table columns
        // are all of string type
        for (int ctr = 0; ctr < colCountInTarget; ctr++)
            if (targetRow[ctr] == DBNull.Value)
                targetRow[ctr] = String.Empty;

        // Now add the null filled row to target datatable
        dtTarget.Rows.Add(targetRow);
    }

    // Return the target datatable
    return dtTarget;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM