如何使用数据集在XML中显示空值 [英] How to display null values in xml using dataset

查看:412
本文介绍了如何使用数据集在XML中显示空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到与xml文档有关的问题,我想将空值(它们不会显示在xml结构上)显示为空字符串或其他内容,

示例:

I''m having an issue relating a xml document, i want to show null values ( they dont show up on xml structure ) as an empty string or something,

Example:

<?xml version="1.0" standalone="yes"?>
<SerialisedData>
  <xs:schema id="SerialisedData" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
    <xs:element name="SerialisedData" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1" msprop:BaseTable.0="TABLE_NAME" msprop:BaseSchema="SCHEMA_NAME">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="FILE" msprop:OraDbType="126" msprop:BaseColumn="FILE" type="xs:string" minOccurs="0" />
                <xs:element name="BBid" msprop:OraDbType="113" msprop:BaseColumn="BBid" type="xs:long" minOccurs="0" />
                <xs:element name="trnid" msprop:OraDbType="113" msprop:BaseColumn="trnid" type="xs:long" minOccurs="0" />
                <xs:element name="stnid" msprop:OraDbType="113" msprop:BaseColumn="stnid" type="xs:long" minOccurs="0" />
                <xs:element name="PIGGYBACK" msprop:OraDbType="126" msprop:BaseColumn="PIGGYBACK" type="xs:string" minOccurs="0" />
                <xs:element name="PAY_TYPE" msprop:OraDbType="126" msprop:BaseColumn="PAY_TYPE" type="xs:string" minOccurs="0" />
                <xs:element name="AMT" msprop:OraDbType="107" msprop:BaseColumn="AMT" type="xs:decimal" minOccurs="0" />
                <xs:element name="POSNUMBER" msprop:OraDbType="126" msprop:BaseColumn="POSNUMBER" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>

<Table1>
  <BBid>503101</BBid>
    <trnid>1377982613101</trnid>
    <stnid>13100</stnid>
    <PAY_TYPE>01</PAY_TYPE>
    <AMT>10000</AMT>
    <POSNUMBER>CCT001</POSNUMBER>
  </Table1>
  <Table1>
  <BBid>573101</BBid>
    <trnid>1377982723101</trnid>
    <stnid>13100</stnid>
    <PAY_TYPE>01</PAY_TYPE>
    <AMT>1000</AMT>
    <POSNUMBER>CCT001</POSNUMBER>
  </Table1>

I want to display like this (including the PIGGYBACK column):

<Table1>
 <BBid>573101</BBid>
    <trnid>1377982763101</trnid>
    <stnid>13100</stnid>
    <PAY_TYPE>01</PAY_TYPE>
    <AMT>9000</AMT>
    <POSNUMBER>CCT001</POSNUMBER>
    <PIGGYBACK></PIGGYBACK>
  </Table1>

<Table1>
 <BBid>573101</BBid>
    <trnid>1377982763101</trnid>
    <stnid>13100</stnid>
    <PAY_TYPE>01</PAY_TYPE>
    <AMT>9000</AMT>
    <POSNUMBER>CCT001</POSNUMBER>
    <PIGGYBACK></PIGGYBACK>
  </Table1>



我的PIGGYBACK 列包含从视图/表(DataSet)中返回的null ,并且在我的代码中进一步引用了此列.

我使用以下代码来编写XML文件:



My PIGGYBACK column contains/returns null from the view/table (DataSet) and I am referencing this column in my code further down.

I use the following to Write the XML file:

DataSet ds = new DataSet("SerialisedData");
        ds.WriteXml(@"C:\FileName.XML", XmlWriteMode.WriteSchema);




要读取文件:




To Read the file:

DataSet ds = new DataSet("SerialisedData");
            ds.ReadXml(@"C:\FileName.XML", XmlReadMode.ReadSchema);




我该怎么办?

预先感谢您的帮助.

谢谢!




How can I do this?

Thank you in advance for your help.

Thanks!

推荐答案

我将创建一个代表记录的类(将所有列作为属性,并提供一个看起来像这样的附加属性):

I would create a class that represents the record (with all of the columns as properties, and provide an additional property that looks something like this):

public XElement AsXElement
{
    get
    {
        XElement value = new XElement("BBid",
                                      new XElement("trnid", this.TrnID.ToString()),
                                      new XElement("stnid", this.StnID.ToString()),
                                      ...
                                      new XElement("PIGGYBACK", (this.PiggyBack == null)?"", this.PiggyBack.ToString())
                                     );
        return value;
    }
}


我一直都这样.

可能会有更好的方法...

我还具有一些扩展功能,这些功能允许我从XML中设置对象的属性,并且它们用于处理空值并在设置过程中提供异常处理.

使用扩展方法来避免XML问题 [ ^ ]


I do it this way all the time.

There may be a better way...

I also have some extension functions that allow me to set the properties in the object from XML and they''re used to handle null values and provide exception handling during the setting process.

Using Extension Methods To Avoid XML Problems[^]


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

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