When there is a null value for a column in a datatable and when you try to convert to xml using the writeXML of the datatable, the XML will not have that element.
For ex: if your datatable is like this
| FirstName | LastName | EmailAddress |
| bob | j | Bob.email@email1.com |
| santa | Banta |
As shown above the second entry doesnt have the email Address.. the generated XML will be
<root>
<table>
<firstname>bob</firstname>
<lastname>j</lastname>
<emailaddress>Bob.email@email1.com</emailaddress>
</table>
<table>
<firstname>santa</firstname>
<lastname>Banta</lastname>
</table>
</root>
as you can see the EmailAddress is missing from the second...
one way to accomplish this is writing string.empty to the null columns... wrote a small mehtod that can do this...
public DataTable writeEmptyStringToNullElelments(DataTable dt){
foreach (DataRow oRow in dt.Rows)
{
for (int colCount = 0; colCount < oRow.ItemArray.Length; colCount++)
{
if (oRow.ItemArray[colCount] == DBNull.Value)
{
oRow.SetField(colCount, string.Empty);
}
}
}
dt.AcceptChanges();
return dt;
}
call the above method before generating the XML
Now the generated XML will be
<root>
<table>
<firstname>bob</firstname>
<lastname>j</lastname>
<emailaddress>Bob.email@email1.com</emailaddress>
</table>
<table>
<firstname>santa</firstname>
<lastname>Banta</lastname>
<emailaddress />
</table>
</root>
Bobby :)
No comments:
Post a Comment