C# XmlTextReader gets: The thread XXXX has exited with code 259 (0x103).

Hi Experts,

I'm trying to learn ASP.net/C# and have run into an issue I'm not able to solve myself.

I'm trying to seed a list of Postal Codes into my own DB from a publicly available XML file - but when I try to run the seed, it is incredibly slow - and I randomly receive an "The thread XXXX has exited with code 259 (0x103)."

I've added some Debug statements to test if the data received is valid, and it is actually adding the data to my local DB.

What might be causing this performance issue? And what might be a better solution? (see code below)

Best regards

My code so far:
        public ActionResult Seed()
            ApplicationDbContext db = new ApplicationDbContext();

            var postalCodes = new List<PostalCode>();

            String URLString = "http://geo.oiorest.dk/postnumre.xml";

            XmlTextReader reader = new XmlTextReader(URLString);

            int fra = 0;
            int til = 0;
            String navn = "";

            while (reader.Read())
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "postnummer")
                    fra = reader.ReadElementContentAsInt();
                    til = reader.ReadElementContentAsInt();
                    Debug.WriteLine("fra: " + fra + " til: " + til);
                    navn = reader.ReadElementContentAsString();

                    if (til > fra)
                        for (int i = 0; i <= til - fra; i++)
                            String postnr = "" + (fra + i);
                            postalCodes.Add(new PostalCode { City = navn, PostalCodeTxt = postnr });

                        postalCodes.Add(new PostalCode { City = navn, PostalCodeTxt = "" + fra });

                    foreach (var temp in postalCodes)




            return RedirectToAction("Index");
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
I would use the built-in serializer:


Download the file to your development system. Store it e.g. under C:\Temp.


Open the Developer Command Prompt for VS2013.


In the prompt, goto C:\Temp (or where you saved your XML).


Run xsd postnumre.xml


Run xsd postnumre.xsd /classesDeveloper prompt and XSD


Create a new project or open an existing one.


Add the file created in step 5 to your solution.Solution explorer


Use the built-in deserializer.
namespace Samples
    using System;
    using System.Xml;
    using System.Xml.Serialization;

    public class Sample
        public static void Main(string[] args)
            const string URL = "http://geo.oiorest.dk/postnumre.xml";
            using (XmlTextReader reader = new XmlTextReader(URL))
                XmlSerializer x = new XmlSerializer(typeof(postnumre));
                postnumre p = (postnumre)x.Deserialize(reader);
                foreach (postnumrePostnummer pn in p.Items)
                    Console.WriteLine(string.Format("nr={0}, href={1}, navn={0}", pn.nr, pn.href, pn.navn));


Open in new window

When the error still occours, then download the file separatly, before feeding it into the deserialization.

And don't forget to add exception handling.
imfpalneAuthor Commented:
Thank you for your reply ste5an, however I would prefer a solution that would allow me to update my own data from an API kind of XML feed - I know that there is rarely changes to a list of Postal Codes, however should that happen, I take it that your solution would require me to re-download and serialize the XML - am I correct?
ste5anSenior DeveloperCommented:
Yes, you are. But it doesn't matters.

Cause your manual XML processing makes also assumptions to the XML file used. The only difference is that in this case it is sufficient to display it in a browser (download) and code the necessary steps (recreate postnumre.cs). So you're basically doing the same, just using different techniques.

But the solution would imho be the same, just download the entire file before processing it.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

imfpalneAuthor Commented:
Hi Ste5an, I've tried your solution - and it works. However, it's not the solution I'm after, let's say I was retrieving data from an XML source that was updated twice a week. With your method, I would have to manually download and prepare the XML file twice a week (assuming that I actually know the update cycle even..) - I need a way to dynamically call the XML whenever I choose to, check my "local" DB to see if there are any changes, and then update my local DB with the changes.

As I found out - the source also offers jSon data if that makes it any easier?
ste5anSenior DeveloperCommented:
Maybe, when it solves your problem.
imfpalneAuthor Commented:
I eventually found this thread, which has the exact answer to my question:


Ste5an, thanks for trying.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imfpalneAuthor Commented:
The answer in this thread is exactly what I was looking for - it speeds up the proces by loading the entire XML to a String and then process the string with xmlreader.read(String)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.