Link to home
Start Free TrialLog in
Avatar of arthrex
arthrexFlag for Germany

asked on

c# - System.XML - Function so slow. Any other suggestions to solve that problem?

   
HI Experts,

I wrote the following function.
I'm calling a website with a special paramter (productid).
I receive an xml-file with the imagename of the products image.
I read this filename and attach it to the img src-path which is known.
but my problem is... it is soooo slow.
Am I doing anything wrong? Is there a faster way?
The pictures are pretty small and not so many. so that can't be the reason.
Please help.
Thanks!!!

call:
imgname = imageresolver.getImageNameFor(item.Key);
imgProduct.Src = @"http://www.address.com/catalog/images/small/" + imgname;
function:
    public string getImageNameFor(string productId)
        {
            string url = "http://www.address.com/default.cfm?productnumber=" + productId;
            System.Net.WebRequest request = System.Net.WebRequest.Create(url);
            System.IO.Stream input = request.GetResponse().GetResponseStream();

            string result = "";

            System.IO.StringWriter writer = new System.IO.StringWriter();

            System.IO.StreamReader streamReader = new System.IO.StreamReader(input);

            XmlTextReader reader = new XmlTextReader(url);
            string name;
            while (reader.Read())
            {        
                name = reader.Name;

                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (name.StartsWith("imagefilename", true, null))
                        {
                            result = reader.ReadElementContentAsString();                          
                        };
                        break;

                }
            }
            streamReader.Close();
            input.Close();

            return result;
        }
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jasonclarke
jasonclarke

Which bit is slow?
Avatar of arthrex

ASKER

Hi Bob and jason,

thanks a lot for your fast answers.
1)you're absoultely right. the code wasn't "clean".
There were rests of my first attempts.
Below the cleaned code.
2)The response time of the website is about 150 ms (ping).
3)the xml-file has only two nodes. one with the productname. one with the imagefilename.  
4)see 1)

@jason what do you mean with "which bit is slow". I'm sorry I don't understand that question.

Thanks a lot!!
arthrex


      public string getImageNameFor(string productId)
        {
            string url = "http://www.address.com/default.cfm?productnumber=" + productId;

            string result = "";
            XmlTextReader reader = new XmlTextReader(url);
            string name;
            while (reader.Read())
            {        
                name = reader.Name;

                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (name.StartsWith("imagefilename", true, null))
                        {
                            result = reader.ReadElementContentAsString();                          
                        };
                        break;

                }
            }
            return result;
        }

 
I meant - which part of the code is causing the whole thing to be slow.  

Is it grabbing the document from the url or is it some aspect of the processing in the while loop?

I am not sure how XmlTextReader behaves with a remote URL - the simple thing to test would be to see if just reading the XML directly into a string and then parsing that using your code is any quicker.
What is the current time to execute that method?

Bob
Avatar of arthrex

ASKER

On the page there are 10pics. each pic 26 KB.
The Page needs about 6-8 sec until it is loaded.
Avatar of arthrex

ASKER

Hi Jason,

I've just tried with a Stream and Stringoperation - not faster.
I'm not sure so far, what in detail causes the site to load so slow.

           System.Net.WebRequest request = System.Net.WebRequest.Create(url);
            System.IO.Stream input = request.GetResponse().GetResponseStream();

            string inputLine;

            string result = "";

            System.IO.StringWriter writer = new System.IO.StringWriter();

            System.IO.StreamReader streamReader = new System.IO.StreamReader(input);
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arthrex

ASKER

At the end it was really the server which was responing to slow.
And I thought my programming is so bad......
Thanks anyway for your help!!