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
You use the single quote
'to surround your attributes. YourRemoveSpecialCharactersfunction does not remove that therefor the XML you create is invalid:is causing the trouble.
A quick fix is to disallow the single quote in
RemoveSpecialCharacters.For ways to automatically escape special characters look here.
In general though you should not be writing XML using a
StringBuilder. Use something likeXmlTextWriterorXDocumentor evendtCSVExcelSource.WriteXml(...).BTW: Your
RemoveSpecialCharactersfunction has lots of issues.elseblock does not do anythingstr[i] >= 'A'does not seem properly balancedstr[i] >= 'A' && str[i] <= 'z'is unusual and allows characters like:[\]^_