Click here to Skip to main content
11,640,910 members (58,680 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
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>

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!
Posted 6-Apr-11 5:46am
Obopeng368

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
    }
}
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[^]
  Permalink  
v2
Comments
Obopeng at 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)

  Print Answers RSS
0 Mika Wendelius 380
1 OriginalGriff 205
2 DamithSL 203
3 Afzaal Ahmad Zeeshan 129
4 CPallini 125
0 Mika Wendelius 370
1 OriginalGriff 205
2 CPallini 125
3 DamithSL 105
4 Afzaal Ahmad Zeeshan 70


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 6 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100