Solved

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

Posted on 2014-11-07
7
1,754 Views
Last Modified: 2014-11-21
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");
        }
0
Comment
Question by:imfpalne
  • 4
  • 3
7 Comments
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
I would use the built-in serializer:

1

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

2

Open the Developer Command Prompt for VS2013.

3

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

4

Run xsd postnumre.xml

5

Run xsd postnumre.xsd /classesDeveloper prompt and XSD

6

Create a new project or open an existing one.

7

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

8

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));
                }
            }

            Console.ReadLine();
        }
    }
}

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.
0
 

Author Comment

by:imfpalne
Comment Utility
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?
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:imfpalne
Comment Utility
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?
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Maybe, when it solves your problem.
0
 

Accepted Solution

by:
imfpalne earned 0 total points
Comment Utility
I eventually found this thread, which has the exact answer to my question:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22739861.html

Ste5an, thanks for trying.
0
 

Author Closing Comment

by:imfpalne
Comment Utility
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)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now