0

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

2
  • Updated the post with sample csv file content from whcih the xml is generated. Commented Jul 28, 2011 at 10:19
  • Code seems fine.Did you google for that particular error ?.. Commented Jul 28, 2011 at 10:48

1 Answer 1

2

You use the single quote ' to surround your attributes. Your RemoveSpecialCharacters function does not remove that therefor the XML you create is invalid:

<row ... City='VETRANS' HOSPITAL' ... />

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 like XmlTextWriter or XDocument or even dtCSVExcelSource.WriteXml(...).

BTW: Your RemoveSpecialCharacters function has lots of issues.

  • The code in the else block does not do anything
  • The parenthesis that starts before str[i] >= 'A' does not seem properly balanced
  • Allowing the range str[i] >= 'A' && str[i] <= 'z' is unusual and allows characters like: [\]^_
Sign up to request clarification or add additional context in comments.

1 Comment

Solved using WriteXml of datatable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.