数据集到xml空值 [英] Dataset to xml Null Values

查看:86
本文介绍了数据集到xml空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,从3个表中获取数据并编写一个xml。

我想写(当记录列为 null value)XML上具有空值的列。例如,如果(Category_name == Null)在xml上写(Null),现在代码跳过该列,甚至在xml上都没有此列。

I have the code below, where from 3 tables I take the data and write an xml.
I want write (when a record column has null value) the column on the xml with null value. 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.

 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);


推荐答案

在此处引用类似的问题- dataSet.GetXml()不会为空列或空白列返回xml

Refer similar question here - dataSet.GetXml() doesn't return xml for null or blank columns

除了此处提到的解决方案之外,您还可以遍历数据集并使用XmlTextWriter编写XML。如果您要处理海量数据,则不建议使用此方法。

Apart from solutions mentioned there, you can also traverse through dataset and write XML using XmlTextWriter. This method is not recommended if you are dealing with huge data.

这篇关于数据集到xml空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆