开发者

shorten xml file loading time

开发者 https://www.devze.com 2022-12-22 06:29 出处:网络
I have the below code. It works fine, but it takes too long to load, well about 30 seconds. I开发者_StackOverflows there anything I can do to shorten this time.

I have the below code. It works fine, but it takes too long to load, well about 30 seconds. I开发者_StackOverflows there anything I can do to shorten this time.

Also, I would like to search the xml file for documents with NAME starting with A,B,C etx. How do I do that?

Many thanks,

    Dim xdoc As New XPathDocument(xt)
   Dim nav As XPathNavigator = xdoc.CreateNavigator()
  Dim expr As XPathExpression
  expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT")

 Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
 namespaceManager.AddNamespace("pf", "http://namespace/")

 expr.SetContext(namespaceManager)

Dim nodes As XPathNodeIterator = nav.Select(expr)

If nodes.Count <> 0 Then

Dim tr As String = Nothing

For Each node As XPathNavigator In nodes

  tr += "<td><a Target='_blank' href='http://www.urltosite.aspx?contract=" & node.SelectSingleNode("pf:ID", namespaceManager).Value & "'>" & node.SelectSingleNode("pf:NAME", namespaceManager).Value & "</a></td>"

  For Each subNode2 As XPathNavigator In node.Select("pf:SUPPLIERS/pf:SUPPLIER", namespaceManager)

 tr += "<td>" & subNode2.SelectSingleNode("pf:SUPPLIERNAME", namespaceManager).Value & "</td>"

  Next

  tr += "<td>" & node.SelectSingleNode("pf:ENDDATE", namespaceManager).Value & "</td>"

 tr += "</tr>"

 Next

 Dim th As String = "<th width='50%'>Name</th><th width='30%'>Supplier</th><th width='20%'>End Date</th>"

 div1.InnerHtml = ("<table width='96%' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>" & th) + tr & "</table>"

 Else

div1.InnerHtml = "No results for your search"

End If

++UPDATE++

Thanks for your help

I've added a StringBuilder to my code instead of the string concatenation. However, the performance didn't change, so I assume the problem is somewhere else.

I forgot to mention in my previous email that the xml data I get comes from a Web service I am consuming. Is there anyhthing I can do to optimise this performance? Many thanks


You must use a StringBuilder. Set the initial capacity of the StringBuilder object to a value that is appropriate.

Question, have you considered transforming the XML with a stylesheet?

Note that the & operator allows you to concatenate different data types into strings. Therefore, your code is doing extra implicit work to convert different objects into strings. Yet, once you use a StringBuilder you will eliminate the need to use these operators.


As for the performance question, profile your code to find out where it spends most of its time, then work from that. StringBuilder instead of string concatenation was already mentioned.

As for your second question "I would like to search the xml file for documents with NAME starting with A,B,C" I am not sure what you are asking as an "xml file" does usually not contain "documents" to search for.

Maybe you have elements named 'document' in your XML document and want to search for those starting with e.g. 'A'; in that case the XPath starts-with function helps e.g. //document[starts-with(., 'A')] selects all elements named 'document' where the string contents starts with 'A'.


Try using XPathExpression objects for the other SelectSingleNode calls. Do these objects before the start of the loop. An expression is valid for the whole document. You don't have to recreate them for each element. Sorry, but I should have noticed this before. This will definitely help.

Once this is done, you may want to time the actual call to the web service using the StopWatch object. Then time your Xml processing.

I do not know how large the data set is or your situation, but using XslCompiledTransform with a stylesheet may be the better solution in regards to performance. (You can store the XslCompiledTransform as an application object, if needed.) (You store the stylesheet as a resource, but as a separate file allows you to edit if needed without re-compiling.) It would also ease maintenance, because you only need to change the stylesheet and not code. Following, is my "standard" method call to output Xml data as Html.

Public Function GatherSummaryPage() As String
    'GatherXmlData (below) returns the XML Data.
    'GatherXsltDocument (below) gets the Xslt stylesheet from resources

    Dim sPage As String
    Dim oResultingXml As New System.IO.StringWriter
    Dim oXslWriter As New System.Xml.XmlTextWriter(oResultingXml)

    Dim oXML As New System.Xml.XmlDocument
    Dim oXSL As New System.Xml.Xsl.XslCompiledTransform

    oXML.LoadXml(GatherXmlData)
    oXSL.Load(New System.Xml.XmlTextReader(New System.IO.StringReader(GatherXsltDocument)), Nothing, Nothing)

    oXSL.Transform(oXML, oXslWriter)

    sPage = oResultingXml.ToString

    oResultingXml.Close()

    GatherSummaryPage = sPage

End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消