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 8699783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:55+00:00 2026-06-13T02:01:55+00:00

I have a while loop going through an XML file, and for one of

  • 0

I have a while loop going through an XML file, and for one of the nodes “url”, there are sometimes invalid values within it. I put a try-catch statement around this to catch any invalid values. The problem is, whenever an invalid value is grabbed the while loop is killed and the program continues on outside of that loop. I need the while loop to continue reading through the rest of the XML file after an invalid value if found.

Here is my code:

        XmlTextReader reader = new XmlTextReader(fileName);
        int tempInt;

        while (reader.Read())
        {
            switch (reader.Name)
            {
                case "url":
                    try
                    {
                        reader.Read();
                        if (!reader.Value.Equals("\r\n"))
                        {
                            urlList.Add(reader.Value);
                        }
                    }
                    catch
                    {                            
                        invalidUrls.Add(urlList.Count);   
                    }
                    break;
            }
        }

I chose not to include the rest of the switch statement as it is not relevant. Here is a sample of my XML:

<?xml version="1.0"  encoding="ISO-8859-1" ?>
<visited_links_list>
    <item>
        <url>http://www.grcc.edu/error.cfm</url>
        <title>Grand Rapids Community College</title>
        <hits>20</hits>
        <modified_date>10/16/2012 12:22:37 PM</modified_date>
        <expiration_date>11/11/2012 12:22:38 PM</expiration_date>
        <user_name>testuser</user_name>
        <subfolder></subfolder>
        <low_folder>No</low_folder>
        <file_position>834816</file_position>
     </item>
</visited_links_list>

The exception I get throughout the code is similar to the following:

“‘ ‘, hexadecimal value 0x05, is an invalid character. Line 3887, position 13.”

  • 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-06-13T02:01:57+00:00Added an answer on June 13, 2026 at 2:01 am

    Observation:

    You’re calling reader.Read() twice for each entry. Once in while(), and once within the case. Do you really mean to skip records? This will cause an exception if there are an odd number of entries in the source XML (since reader.Read() advances the pointer within the XML stream to the next item), but that exception will not be caught because it happens outside of your try...catch.

    Beyond that:

    reader.Read(); /// might return false, but no exception, so keep going...
    
    if (!reader.Value.Equals("\r\n")) /// BOOM if the previous line returned false, which you ignored
    { 
        urlList.Add(reader.Value); 
    } 
    /// reader is now in unpredictable state
    

    Edit

    At the risk of writing a novel-length answer…

    The error you’re receiving

    “‘ ‘, hexadecimal value 0x05, is an invalid character. Line 3887, position 13.”

    indicates that your source XML is malformed, and somehow wound up with a ^E (ASCII 0x05) at the specified position. I’d have a look at that line. If you’re getting this file from a vendor or a service, you should have them fix their code. Correcting that, and any other malformed content within your XML, should correct issue that you’re seeing.

    Once that is fixed, your original code should work. However, using XmlTextReader for this isn’t the most robust of solutions, and involves building some code that Visual Studio will happily generate for you:

    In VS2012 (I don’t have VS2010 installed any more, but it should be the same process):

    • Add a sample of the XML to your solution

    • In the properties for that file, set the CustomTool to “MSDataSetGenerator” (without the quotes)

    • The IDE should generate a .designer.cs file, containing a serializable class with a field for each item in the XML. (If not, right-click on the XML file in the solution explorer and select “Run Custom Tool”.)

    enter image description here

    • Use code like the following to load XML with the same schema as your sample at runtime:

      /// make sure the XML doesn't have errors, such as non-printable characters
      private static bool IsXmlMalformed(string fileName)
      {
          var reader = new XmlTextReader(fileName);
          var result = false;
      
          try
          {
              while (reader.Read()) ;
          }
          catch (Exception e)
          {
              result = true;
          }
      
          return result;
      }
      
      /// Process the XML using deserializer and VS-generated XML proxy classes
      private static void ParseVisitedLinksListXml(string fileName, List<string> urlList, List<int> invalidUrls)
      {
          if (IsXmlMalformed(fileName))
              throw new Exception("XML is not well-formed.");
      
          using (var textReader = new XmlTextReader(fileName))
          {
              var serializer = new XmlSerializer(typeof(visited_links_list));
      
              if (!serializer.CanDeserialize(textReader))
                  throw new Exception("Can't deserialize this XML. Make sure the XML schema is up to date.");
      
              var list = (visited_links_list)serializer.Deserialize(textReader);
      
              foreach (var item in list.item)
              {
                  if (!string.IsNullOrEmpty(item.url) && !item.url.Contains(Environment.NewLine))
                      urlList.Add(item.url);
                  else
                      invalidUrls.Add(urlList.Count);
              }
          }
      }
      

    You can also do this with the XSD.exe tool included with the Windows SDK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Essentially, I have a Do..While loop going through some lines from a text file.
I have my loop going through vector's elements. While in this loop some of
While going through parsing a xml doc i have used multiple if-else to parse
I have a while loop that is going through and displaying an RSS icon
The while loop I have while reading in from a file doesn't break. I'm
I have a while loop that loops through 3 results and echo's these out
I'm going through a rigorous memory based issue while iterating over a loop performing
I have a while loop that goes while a BuffedReader still has data, what
hi i have a while loop: var i = 0; while(i < 20) {
So with pygame you have a while loop that loops continuously, then your event

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.