如何将数据表导出到XML与所有列的属性?如何将、数据表、属性、导出到

2023-09-03 02:02:26 作者:宣我会死啊

问:我导出System.Data.DataTable到XML。 到目前为止,它工作正常。 但我想在属性中的所有数据,它工作正常,良好。 但我现在的问题,如果在一列,所有行空,没有空的属性写入。 所以,如果我读取XML回到一个数据表,它缺少此列...

如何强制写入所有列,即使它们是空的? (数据类型不necessarely字符串)

 公共无效ExportTable(字符串strDirectory,数据表DTT)
{
    使用(System.Data.DataSet中DS =新System.Data.DataSet中()){
        字符串strTable = dtt.TableName;

        ds.Tables.Add(DTT);
        ds.DataSetName = strTable;

        //将数据移动到属性
        的foreach(在ds.Tables数据表DT){

            的foreach(DataColumn的DC IN dt.Columns){
                dc.ColumnMapping = MappingType.Attribute;
            }

        }

        System.Xml.XmlWriterSettings设置=新System.Xml.XmlWriterSettings();
        settings.Indent = TRUE;
        //settings.Encoding = System.Text.Encoding.GetEncoding(ISO-8859-1)
        settings.Encoding = System.Text.Encoding.UTF8;
        settings.CloseOutput = TRUE;
        settings.CheckCharacters = TRUE;
        settings.NewLineChars =\ r \ N的;
        // VBCR和放大器; vbLf

        //写为UTF-8与缩进
        使用(System.Xml.XmlWriter W = System.Xml.XmlWriter.Create(System.IO.Path.Combine(strDirectory,strTable +的.xml),设置)){

            //去掉时区
            的foreach(在ds.Tables数据表DT){

                的foreach(DataColumn的DC IN dt.Columns){

                    如果(object.ReferenceEquals(dc.DataType的typeof(DateTime的))){
                        dc.DateTimeMode = DataSetDateTime.Unspecified;
                    }

                }

            }

            ds.Tables [0] .WriteXml(瓦特,XmlWriteMode.IgnoreSchema);
            w.Flush();
            w.Close();
        }
        //W¯¯

    }
    // DS

}
// ExportTable
 

VB.NET原:

 公用Sub ExportTable(strDirectory作为字符串,DTT作为数据表)
        使用DS作为新System.Data.DataSet中()
            昏暗strTable作为字符串= dtt.TableName

            ds.Tables.Add(DTT)
            ds.DataSetName = strTable

            将数据移动到属性
            对于每一个DT作为数据表在ds.Tables

                对于每个DC作为的DataColumn在dt.Columns
                    dc.ColumnMapping = MappingType.Attribute
                接下来DC

            接下来DT

            昏暗的设置作为新System.Xml.XmlWriterSettings()
            settings.Indent = TRUE
            settings.Encoding = System.Text.Encoding.GetEncoding(ISO-8859-1)
            settings.Encoding = System.Text.Encoding.UTF8
            settings.CloseOutput = TRUE
            settings.CheckCharacters = TRUE
            settings.NewLineChars = vbCrLf'VBCR和放大器; vbLf

            写为UTF-8与缩进
            在W作为System.Xml.XmlWriter = System.Xml.XmlWriter.Create(System.IO.Path.Combine(strDirectory,strTable和放大器;的.xml),设置)

                带出时区
                对于每一个DT作为数据表在ds.Tables

                    对于每个DC作为的DataColumn在dt.Columns

                        如果dc.DataType是的GetType(日期时间),然后
                            dc.DateTimeMode = DataSetDateTime.Unspecified
                        结束如果

                    接下来DC

                接下来DT

                ds.Tables(0).WriteXml(W,XmlWriteMode.IgnoreSchema)
                w.Flush()
                w.Close()
            使用完'W

        使用完DS

    完子'ExportTable
 

解决方案

每个XML属性必须被指定括在一对单或双引号的值。有以纯文本格式来表示一个NULL值不相等的。一对引号没有价值重新present一个空字符串是不一样的NULL值。因此,只有这样才能重新present一个空的属性是忽略的属性。

如何直接在web上编辑指定数据表的字段属性

这意味着你要么需要设置 AllowDBNull 为false,并指定一个合适的默认值上的DataColumn,或包括架构。

此外,请参阅处理Null值(ADO.NET)。,尤其是本节其说明了其行为:

  

此外,下列规则适用于实例   。DataRow的[的ColumnName]空任务:

     

1,默认的缺省值是对的DBNull.Value之外的所有强类型的空列其中,这是适当的强类型   空值。

     序列化到XML文件中

2.Null值永远不会写出(如XSI:无)。

     

3,所有非空值,包括默认值,总是写出来,而序列化到XML。这不同于XSD / XML语义,其中一个   空值(XSI:无)是明确的,默认值是隐式的(如果   没有present在XML中,验证解析器可以从相关的得到它   XSD架构)。与此相反的是真正的一个DataTable:一个空值   隐式和默认值是显式的。

     

4.所有从XML输入读取的行缺少的列值赋值为NULL。使用NEWROW或类似的方法创建的行分配的   的DataColumn的默认值。

     

5,ISNULL方法返回true,为的DBNull.Value和INullable.Null。

Question: I'm exporting a System.Data.DataTable to XML. So far it works fine. But I want to have all the data in attributes, which works fine as well. But my problem now, if in one column, all rows are NULL, no empty attributes are written. So if I read the XML back to a DataTable, it lacks this column...

How can I force write all columns even when they are empty ? (DataType not necessarely string)

public void ExportTable(string strDirectory, DataTable dtt)
{
    using (System.Data.DataSet ds = new System.Data.DataSet()) {
        string strTable = dtt.TableName;

        ds.Tables.Add(dtt);
        ds.DataSetName = strTable;

        // Move data to attributes 
        foreach (DataTable dt in ds.Tables) {

            foreach (DataColumn dc in dt.Columns) {
                dc.ColumnMapping = MappingType.Attribute;
            }

        }

        System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
        settings.Indent = true;
        //settings.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1") 
        settings.Encoding = System.Text.Encoding.UTF8;
        settings.CloseOutput = true;
        settings.CheckCharacters = true;
        settings.NewLineChars = "\r\n";
        // vbCr & vbLf 

        // Write as UTF-8 with indentation 
        using (System.Xml.XmlWriter w = System.Xml.XmlWriter.Create(System.IO.Path.Combine(strDirectory, strTable + ".xml"), settings)) {

            // Strip out timezone 
            foreach (DataTable dt in ds.Tables) {

                foreach (DataColumn dc in dt.Columns) {

                    if (object.ReferenceEquals(dc.DataType, typeof(DateTime))) {
                        dc.DateTimeMode = DataSetDateTime.Unspecified;
                    }

                }

            }

            ds.Tables[0].WriteXml(w, XmlWriteMode.IgnoreSchema);
            w.Flush();
            w.Close();
        }
        // w 

    }
    // ds 

}
// ExportTable 

VB.NET original:

 Public Sub ExportTable(strDirectory As String, dtt As DataTable)
        Using ds As New System.Data.DataSet()
            Dim strTable As String = dtt.TableName

            ds.Tables.Add(dtt)
            ds.DataSetName = strTable

            ' Move data to attributes
            For Each dt As DataTable In ds.Tables

                For Each dc As DataColumn In dt.Columns
                    dc.ColumnMapping = MappingType.Attribute
                Next dc

            Next dt

            Dim settings As New System.Xml.XmlWriterSettings()
            settings.Indent = True
            'settings.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1")
            settings.Encoding = System.Text.Encoding.UTF8
            settings.CloseOutput = True
            settings.CheckCharacters = True
            settings.NewLineChars = vbCrLf ' vbCr & vbLf

            ' Write as UTF-8 with indentation
            Using w As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(System.IO.Path.Combine(strDirectory, strTable & ".xml"), settings)

                ' Strip out timezone
                For Each dt As DataTable In ds.Tables

                    For Each dc As DataColumn In dt.Columns

                        If dc.DataType Is GetType(DateTime) Then
                            dc.DateTimeMode = DataSetDateTime.Unspecified
                        End If

                    Next dc

                Next dt

                ds.Tables(0).WriteXml(w, XmlWriteMode.IgnoreSchema)
                w.Flush()
                w.Close()
            End Using ' w

        End Using ' ds

    End Sub ' ExportTable

解决方案

Every XML attribute must be assigned a value that is enclosed in a pair of single or double quotation marks. There is no equivalent in plain text to denote a NULL value. A pair of quotation marks with no value to represent an empty string is not the same as a NULL value. Therefore, the only way to represent a NULL attribute is to omit the attribute.

This means that you will need to either set AllowDBNull to false and assign a suitable DefaultValue on the DataColumn, or include the schema.

Also, see Handling Null Values (ADO.NET)., particularly this section which explains the behavior:

In addition, the following rules apply for an instance of DataRow.["columnName"] null assignments:

1.The default default value is DbNull.Value for all except the strongly typed null columns where it is the appropriate strongly typed null value.

2.Null values are never written out during serialization to XML files (as in "xsi:nil").

3.All non-null values, including defaults, are always written out while serializing to XML. This is unlike XSD/XML semantics where a null value (xsi:nil) is explicit and the default value is implicit (if not present in XML, a validating parser can get it from an associated XSD schema). The opposite is true for a DataTable: a null value is implicit and the default value is explicit.

4.All missing column values for rows read from XML input are assigned NULL. Rows created using NewRow or similar methods are assigned the DataColumn's default value.

5.The IsNull method returns true for both DbNull.Value and INullable.Null.