[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-08-03
9
Medium Priority
?
1,122 Views
Last Modified: 2012-08-14
   
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;
        }
0
Comment
Question by:arthrex
  • 4
  • 3
  • 2
9 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 800 total points
ID: 19624331
1) What does the StreamReader do?

2) How fast does the web site respond?

3) How large is the XML response?

4) You are using an HttpWebRequest, but it doesn't really seem to be connected to the process.  What is it doing?

Bob
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 19624339
Which bit is slow?
0
 

Author Comment

by:arthrex
ID: 19624475
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;
        }

 
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 9

Expert Comment

by:jasonclarke
ID: 19624690
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19625001
What is the current time to execute that method?

Bob
0
 

Author Comment

by:arthrex
ID: 19625041
On the page there are 10pics. each pic 26 KB.
The Page needs about 6-8 sec until it is loaded.
0
 

Author Comment

by:arthrex
ID: 19625047
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);
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 1200 total points
ID: 19625309
Hi,

what I was suggesting was that you try something like this:

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

            StreamReader streamReader = new StreamReader(input);
            string responseFromServer = streamReader.ReadToEnd();

            StringReader stringReader = new StringReader(responseFromServer);
            XmlTextReader reader = new XmlTextReader(stringReader);

where you read the entire response into a string before processing it - to ensure that the problem is not caused by many network round trips...
0
 

Author Comment

by:arthrex
ID: 19654680
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!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 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