缩短xml文件加载时间

时间:2010-03-03 17:36:34

标签: xml xpath

我有以下代码。它工作正常,但加载时间太长,大约30秒。我有什么办法可以缩短这个时间。

另外,我想在xml文件中搜索NAME以A,B,C etx开头的文档。我该怎么做?

非常感谢,

    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

感谢您的帮助

我已经在我的代码中添加了一个StringBuilder而不是字符串连接。但是,性能没有改变,所以我认为问题出在其他地方。

我忘记在我之前的电子邮件中提到我得到的xml数据来自我正在使用的Web服务。我能做些什么来优化这种性能? 非常感谢

3 个答案:

答案 0 :(得分:1)

您必须使用StringBuilder。将StringBuilder对象的初始容量设置为适当的值。

问题,您是否考虑过使用样式表转换XML?

请注意&amp;运算符允许您将不同的数据类型连接成字符串。因此,您的代码正在进行额外的隐式工作以将不同的对象转换为字符串。但是,一旦使用StringBuilder,就无需使用这些运算符。

答案 1 :(得分:0)

至于性能问题,请对您的代码进行分析,以找出它花费大部分时间的时间,然后从中开始工作。已经提到过StringBuilder而不是字符串连接。

至于你的第二个问题“我想在xml文件中搜索NAME以A,B,C开头的文件”我不确定你要问的是什么,因为“xml文件”通常不包含“文件”搜索。

也许你的XML文档中有名为'document'的元素,并且想要搜索那些以例如'一个';在这种情况下XPath启动 - 功能帮助例如//document[starts-with(., 'A')]选择名为'document'的所有元素,其中字符串内容以'A'开头。

答案 2 :(得分:0)

尝试将XPathExpression对象用于其他SelectSingleNode调用。在循环开始之前执行这些对象。表达式对整个文档有效。您不必为每个元素重新创建它们。 很抱歉,但我之前应该注意到这一点。这肯定有帮助。

完成此操作后,您可能希望使用StopWatch对象计算对Web服务的实际调用时间。然后计算你的Xml处理时间。

我不知道数据集的大小或您的情况,但将XslCompiledTransform与样式表一起使用可能是性能方面的更好解决方案。 (如果需要,您可以将XslCompiledTransform存储为应用程序对象。)(您将样式表存储为资源,但作为单独的文件,您可以根据需要进行编辑而无需重新编译。)它还可以简化维护,因为您只需要需要更改样式表而不是代码。以下是我的“标准”方法调用将Xml数据输出为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