Dataset.GetXML() method doesn't Return Null Values. Any Solution?

Discussion in 'ASP .Net' started by Nanda, Nov 29, 2005.

  1. Nanda

    Nanda Guest

    Hi All,

    Thanks for u r Valuable postings.
    Here is a Problem for me.

    I need to reprsent the NULL value of a column in Database in an XML
    File as follows.

    <Employees>
    <Employee>
    <Name>John</Name>
    </Addr>
    <City>NewYork</City>
    </Employee>
    </Employees>

    Here </Addr> represents a null field in Database.
    But using Dataset.GetXML() method I am getting the XML O/p as follows.
    <Employees>
    <Employee>
    <Name>John</Name>
    <City>NewYork</City>
    </Employee>
    </Employees>

    Can Any body help me from this situation?
    Thanks in Advance
    Regards
    Nanda
     
    Nanda, Nov 29, 2005
    #1
    1. Advertising

  2. Nanda

    Kalpesh Guest

    Hi,

    This is because it is containing NULL (DBNull.Value) - it is being
    ignored.
    Maybe, you can traverse for column having DBNull.Value & make it blank

    Then, I guess it should emit XML the way you want

    HTH
    Kalpesh
     
    Kalpesh, Nov 29, 2005
    #2
    1. Advertising

  3. Nanda

    Mohit_Aren

    Joined:
    May 1, 2008
    Messages:
    2
    GetXML method with null columns

    private void setNullToDefVals(ref DataSet dsXML)
    {
    try
    {
    if(dsXML.Tables.Count > 0)
    {
    for(int index = 0; index < dsXML.Tables.Count; index++)
    {
    if(dsXML.Tables[index].Rows.Count > 0)
    {
    for(int rindex= 0; rindex < dsXML.Tables[index].Rows.Count; rindex++)
    {
    for(int cindex=0; cindex < dsXML.Tables[index].Columns.Count; cindex++)
    {
    if(dsXML.Tables[index].Rows[rindex][cindex] == System.DBNull.Value)
    {
    DateTime defdate = new DateTime(2000,1,1);
    if(dsXML.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.String"))
    dsXML.Tables[index].Rows[rindex][cindex] = "";
    else if(dsXML.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.Int32"))
    dsXML.Tables[index].Rows[rindex][cindex] = 0;
    else if(dsXML.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.DateTime"))
    dsXML.Tables[index].Rows[rindex][cindex] = defdate;
    else if(dsXML.Tables[index].Columns[cindex].DataType == System.Type.GetType("System.Decimal"))
    dsXML.Tables[index].Rows[rindex][cindex] = 0.0;
    else
    {
    // dsXML.Tables[index].Columns[cindex].DataType = System.Type.GetType("System.String");
    // dsXML.Tables[index].Rows[rindex][cindex] = "";
    }
    }
    }
    }
    }
    }
    }
    }
    catch(Exception ex)
    {
    int i =0;
    }
    }
     
    Mohit_Aren, May 1, 2008
    #3
  4. Nanda

    Mohit_Aren

    Joined:
    May 1, 2008
    Messages:
    2
    Blank tags for DataSet GetXML for null values

    Can also use this function before adding default values to get only valuesless tags:

    private void createVarcharColumns(ref DataSet dsXML)
    {
    try
    {
    if (dsXML.Tables.Count > 0)
    {
    for (int index = 0; index < dsXML.Tables.Count; index++)
    {
    if (dsXML.Tables[index].Rows.Count > 0)
    {
    for (int cindex = 0; cindex < dsXML.Tables[index].Columns.Count; cindex++)
    {
    if (dsXML.Tables[index].Columns[cindex].DataType != System.Type.GetType("System.String"))
    {
    string oldColName = dsXML.Tables[index].Columns[cindex].ColumnName;
    if (oldColName != "LOCATIONID" && oldColName != "SUBLOCATIONID")
    {
    DataColumn objColmn = new DataColumn(dsXML.Tables[index].Columns[cindex].ColumnName + "Def", System.Type.GetType("System.String"));
    dsXML.Tables[index].Columns.Add(objColmn);
    string newColName = objColmn.ColumnName;
    int newcindex = getColumnIndex(dsXML.Tables[index], newColName);
    for (int rindex = 0; rindex < dsXML.Tables[index].Rows.Count; rindex++)
    {
    if (dsXML.Tables[index].Rows[rindex][cindex] == System.DBNull.Value)
    {
    dsXML.Tables[index].Rows[rindex][newcindex] = "";
    }
    else
    dsXML.Tables[index].Rows[rindex][newcindex] = dsXML.Tables[index].Rows[rindex][cindex].ToString();
    }
    dsXML.Tables[index].Columns.Remove(dsXML.Tables[index].Columns[cindex].ColumnName);
    newcindex = getColumnIndex(dsXML.Tables[index], newColName);
    dsXML.Tables[index].Columns[newcindex].ColumnName = oldColName;
    cindex--;
    }
    }
    }
    }
    }
    }
    }
    catch (Exception ex)
    {
    }
    }
     
    Mohit_Aren, May 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dan byers
    Replies:
    1
    Views:
    611
    Hans =?iso-8859-1?Q?J=F8rgen?= Jakobsen
    Mar 6, 2004
  2. =?Utf-8?B?S2FzaHlhcA==?=

    Problem with dataset.getxml

    =?Utf-8?B?S2FzaHlhcA==?=, Apr 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    6,266
    Cor Ligthert
    Apr 13, 2004
  3. Replies:
    0
    Views:
    4,190
  4. Carl
    Replies:
    21
    Views:
    993
    Patricia Shanahan
    Aug 24, 2006
  5. lucky

    Probem with DataSet.GetXml()

    lucky, Oct 26, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    424
    Lucky
    Oct 26, 2006
Loading...

Share This Page