문제

내가 원래 이 질문에 대 RefactorMyCode, 지만,없어 응답이...

기본적으로 나는 그를 로드하려고 하는 XmlNodeListXmlDocument 가 있다면 더 많은 효율적인 방법보 보안 시스템을 제공합니다.

Private Function GetPreviousMonthsXml(ByVal months As Integer, ByVal startDate As Date, ByVal xDoc As XmlDocument, ByVal path As String, ByVal nodeName As String) As XmlDocument
    '' build xpath string with list of months to return
    Dim xp As New StringBuilder("//")
    xp.Append(nodeName)
    xp.Append("[")
    For i As Integer = 0 To (months - 1)
      '' get year and month portion of date for datestring
      xp.Append("starts-with(@Id, '")
      xp.Append(startDate.AddMonths(-i).ToString("yyyy-MM"))
      If i < (months - 1) Then
        xp.Append("') or ")
      Else
        xp.Append("')]")
      End If
    Next

    '' *** This is the block that needs to be refactored ***
    '' import nodelist into an xmldocument
    Dim xnl As XmlNodeList = xDoc.SelectNodes(xp.ToString())
    Dim returnXDoc As New XmlDocument(xDoc.NameTable)
    returnXDoc = xDoc.Clone()
    Dim nodeParents As XmlNodeList = returnXDoc.SelectNodes(path)
    For Each nodeParent As XmlNode In nodeParents
      For Each nodeToDelete As XmlNode In nodeParent.SelectNodes(nodeName)
        nodeParent.RemoveChild(nodeToDelete)
      Next
    Next

    For Each node As XmlNode In xnl
      Dim newNode As XmlNode = returnXDoc.ImportNode(node, True)
      returnXDoc.DocumentElement.SelectSingleNode("//" & node.ParentNode.Name & "[@Id='" & newNode.Attributes("Id").Value.Split("-")(0) & "']").AppendChild(newNode)
    Next

    '' *** end ***
    Return returnXDoc
End Function
도움이 되었습니까?

해결책

Dim returnXDoc As New XmlDocument(xDoc.NameTable)
returnXDoc = xDoc.Clone()

첫 번째 여기에 라인이 중복-당신은 당신의 인스턴스를 만들 XmlDocument,다음 재할당하는 변수:

Dim returnXDoc As XmlDocument = xDoc.Clone()

이 같은 작업을 수행합니다.

으로 보고 당신을 삽입하는 각 XmlNode 에서 당신의 노드 목록으로 다른 장소에서 새로운 XmlDocument 다음을 나는 볼 수 없습할 수 있는 방법을 가능하게 이렇게 다른 어떤 방법입니다.

가 더 빠를 수 있습니다 XPath 식 쓸 수 있다,예를 들어 사전중인 XPath 표현으로"//"은 거의 항상 느린 방법으로 뭔가를 하는 경우에 특히 XML 은 잘 구성되어 있습니다.당신이 하지 않은 다음과 같 XML 수 없습니다 그래서 정말이에 더욱 그러나입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top