Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
<?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>


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:

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



To Read the file:

ASM
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!
Posted

1 solution

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

C#
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;
    }
}

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[^]
 
Share this answer
 
v2
Comments
Obopeng 6-Apr-11 12:13pm    
I am not sure what will be the next step from here onwards. Just to add on my question. What I am trying to do is that, I am querying my database which takes time due to large volume of data. For my testing purpose to eliminate the time delay of querying the data from the db into a Datagrid, when I get the data for the first time I also want to write the data to XML file and read the XML data for my further testing. My DataSet contains all the required columns (including "PIGGYBACK"), but column "PIGGYBACK" is not written to the XML file because it returns NULL from the DB table, as shown in the above post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900