Avatar of imfpalne
imfpalne

asked on 

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
Ian

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")
                {
                    reader.ReadToDescendant("fra");
                    fra = reader.ReadElementContentAsInt();
                    reader.MoveToContent();
                    til = reader.ReadElementContentAsInt();
                    Debug.WriteLine("fra: " + fra + " til: " + til);
                    reader.MoveToContent();
                    navn = reader.ReadElementContentAsString();
                    Debug.WriteLine(navn);

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

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

                    foreach (var temp in postalCodes)
                    {
                        db.PostalCodes.Add(temp);
                    }


                    db.SaveChanges();



                }

            }


            return RedirectToAction("Index");
        }
C#ASP.NETXML

Avatar of undefined
Last Comment
imfpalne

8/22/2022 - Mon