Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
缩短xml文件加载时间_Xml_Xpath - Fatal编程技术网

缩短xml文件加载时间

缩短xml文件加载时间,xml,xpath,Xml,Xpath,我有下面的代码。它工作正常,但加载时间太长,大约30秒。我能做些什么来缩短这段时间吗 另外,我想在xml文件中搜索名称以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:CO

我有下面的代码。它工作正常,但加载时间太长,大约30秒。我能做些什么来缩短这段时间吗

另外,我想在xml文件中搜索名称以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
Dim xdoc作为新的XPathDocument(xt)
作为XPathNavigator=xdoc.CreateNavigator()的Dim nav
Dim expr作为XPathExpression
expr=nav.Compile(“/pf:CONTRACTS/pf:CONTRACT”)
Dim namespaceManager作为XmlNamespaceManager=新的XmlNamespaceManager(nav.NameTable)
namespaceManager.AddNamespace(“pf”http://namespace/")
expr.SetContext(名称空间管理器)
将节点变暗为XPathNodeIterator=nav.Select(expr)
如果节点数为0,则
Dim tr As String=Nothing
作为节点中的XPathNavigator用于每个节点
tr+=“”
对于节点中作为XPathNavigator的每个子节点2。选择(“pf:SUPPLIERS/pf:SUPPLIER”,namespaceManager)
tr+=“”&subNode2。选择SingleNode(“pf:SUPPLIERNAME”,namespaceManager)。值&“
下一个
tr+=“”&node。选择SingleNode(“pf:ENDDATE”,namespaceManager)。值&“
tr+=“”
下一个
Dim th As String=“名称供应商结束日期”
div1.InnerHtml=(“”&th)+tr&“
其他的
div1.InnerHtml=“没有搜索结果”
如果结束
++UPDATE++

谢谢你的帮助

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

我忘了在上一封电子邮件中提到,我得到的xml数据来自我正在使用的Web服务。我能做些什么来优化这个性能?
非常感谢

至于性能问题,请分析您的代码,找出它在哪里花费了大部分时间,然后从中着手。已经提到了StringBuilder而不是字符串连接

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


可能您的XML文档中有名为“document”的元素,并且希望搜索以“A”开头的元素;在这种情况下,XPath以函数开头会有帮助,例如,
//document[以(,'A')开头]
选择所有名为'document'的元素,其中字符串内容以'A'开头。

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

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


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

尝试对其他SelectSingleNode调用使用XPathExpression对象。在循环开始之前执行这些操作。表达式对整个文档有效。您不必为每个元素重新创建它们。对不起,我以前应该注意到的。这肯定会有帮助

完成后,您可能希望使用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

如果这是转换XML的方法,那么应该使用适当大小的
StringBuilder
而不是
String
来执行所有这些连接。从web服务获取文件需要多长时间?如果获取文件需要29秒,执行此代码需要1秒,那么问题不在于代码。另外,如果您可以在1秒内针对文件的本地副本运行此代码,并且您可以在几秒钟内从服务器获取文件,然后首先复制文件本地副本,然后解析本地副本。您好,非常感谢您的帮助。我添加了一个StringBuilder,而不是以前的字符串连接。然而,表演没有改变。我忘了在我的帖子中提到我使用的xml文件是通过web服务获得的。这就是问题所在吗?我如何优化这一点?非常感谢