Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:05:51+00:00 2026-05-24T04:05:51+00:00

I have to create xml after reading from a datatable, in C# application. This

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:05:51+00:00Added an answer on May 24, 2026 at 4:05 am

    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: [\]^_
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PHP5 to create XML files. I have code like this: $doc =
I need to create a local xml file from a rails application and then
I have generated a XML file automatically from the below code. if (isset($_POST[song])&& $_POST['song']
i have created an xml file from my c# application i want to use
I have a database in ISO-8859-2 format, but I need to create XML in
I have been thinking about the optimal way to create an XML file using
I have a XML schema that I will need to create Java classes for.
I have an XML file (on the left) and I want to create multiple
I have an XML file that I used to create an XSD File and
I have a source XML file that is used to create C# files that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.