I have to create xml after reading from a datatable, in C# application. This logic is working fine except for “’” and “&”. When these special characters are encountered, there happened error in generated xml. The error is “Required white space was missing”
What are the changes to be made for the following code to make it working for the above mentioned special characters?
Framework: .Net 3.0
Note: The code given below explains what all are valid characters and what are invalid (special character).
Note: The datatable is read from a csv file which looks like
ZipCode,City,State,County
92357,VETRANS' HOSPITAL,CA,SAN BERDINO
36675,MOBLE P&DC,AL,MOBLE
Code folllows....
StringBuilder xmlForCSV = new StringBuilder();
xmlForCSV.Append("<root>");
foreach (DataRow excelRow in dtCSVExcelSource.Rows)
{
string zipCode = RemoveSpecialCharacters(excelRow["ZipCode"].ToString());
string city = RemoveSpecialCharacters(excelRow["City"].ToString());
string state = RemoveSpecialCharacters(excelRow["State"].ToString());
string county = RemoveSpecialCharacters(excelRow["County"].ToString());
xmlForCSV.Append("<row ZipCode='" + zipCode +
"' City='" + city +
"' State='" + state +
"' County='" + county + "'/>");
}
xmlForCSV.Append("</root>");
The function is
private string RemoveSpecialCharacters(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if ((str[i] >= '0' && str[i] <= '9') || (str[i] >= 'A' && str[i] <= 'z' || (str[i] == '.' || str[i] == '_' || str[i] == '-' || str[i] == ' ' || str[i] == '(' || str[i] == ')' || str[i] == '/' || str[i] == '\\' || str[i] == '\'' || str[i] == '#')))
{
sb.Append(str[i]);
}
else
{
if (str[i] == '&')
{
string ampersand = System.Convert.ToString(str[i]);
}
else
{
string specialCharacter = System.Convert.ToString(str[i]);
}
}
}
return sb.ToString();
}
Thanks
Lijo