Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
XML node attribute vanishes
Message
From
24/12/2012 05:33:47
 
 
To
24/12/2012 05:21:43
General information
Forum:
ASP.NET
Category:
XML
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01560521
Message ID:
01560531
Views:
26
>>>>>Cloning was the way to go. This is the new code. However, I would like to know why I have to clone to avoid this situation.
>>>>>
>>>>>
>>>>>        ' Get a XML node attribute
>>>>>        ' expC1 Node
>>>>>        ' expC2 Attribute
>>>>>        Public Function GetXMLNodeAttributeValue(ByVal tcNode As String, ByVal tcAttribute As String) As Boolean
>>>>>            Dim llFound As Boolean = False
>>>>>            Dim loAttribute As XmlAttribute = Nothing
>>>>>
>>>>>            ' Reset the values
>>>>>            cMessage = ""
>>>>>            cNodeAttribute = ""
>>>>>
>>>>>            ' If we have a content
>>>>>            If Not oXmlAttributeCollection Is Nothing Then
>>>>>                oXmlAttributeCollection.RemoveAll()
>>>>>            End If
>>>>>
>>>>>            ' Select the node
>>>>>            If oXmlNameSpaceManager Is Nothing Then
>>>>>                oXmlNode = oXmlDocument.SelectSingleNode(tcNode)
>>>>>            Else
>>>>>                oXmlNode = oXmlDocument.SelectSingleNode(tcNode, oXmlNameSpaceManager)
>>>>>            End If
>>>>>
>>>>>            ' If the node does not exist
>>>>>            If oXmlNode Is Nothing Then
>>>>>                cMessage = oApp.StrTran(cNodeDoesNotExist, "##Node##", tcNode)
>>>>>                Return False
>>>>>            End If
>>>>>
>>>>>            ' Assign the new attributes to this object
>>>>>            oXmlAttributeCollection = oXmlNode.Clone.Attributes
>>>>>
>>>>>            ' Scan the attribute collection
>>>>>            For Each loAttribute In oXmlNode.Attributes
>>>>>
>>>>>                ' If this is the one
>>>>>                If UCase(loAttribute.Name) = UCase(tcAttribute) Then
>>>>>                    cNodeAttribute = loAttribute.Value
>>>>>                    llFound = True
>>>>>                    Exit For
>>>>>                End If
>>>>>
>>>>>            Next
>>>>>
>>>>>            ' If we did not find it
>>>>>            If Not llFound Then
>>>>>                cMessage = oApp.StrTran(cAttributeDoesNotExist, "##Attribute##", tcAttribute)
>>>>>                Return False
>>>>>            End If
>>>>>
>>>>>            Return True
>>>>>        End Function
>>>>>
Well, you certainly shouldn't have to clone (assuming the XmlDocument and the NameSpaceManager do not change - but since the method references objects which are external it's a bit difficult to know what else might be going on.
>>>
>>>
>>>I think the way it is written, he has to clone
>>>
>>>Better would be to take this part out
>>>
>>> ' If we have a content
>>>>            If Not oXmlAttributeCollection Is Nothing Then
>>>>                oXmlAttributeCollection.RemoveAll()
>>>>            End If
>>>
>>>
>>>Then he doesn't need cloning at all
>>
>>But the relevance of oXmlAttributeCollection is not obvious in the listing - and not used at all in the search ?
>>In essence:
    private static bool GetXMLNodeAttributeValue(string tcNode, string tcAttribute)
>>    {
>>      var v = oXmlDocument.SelectSingleNode(tcNode);
>>      if (v == null) return false;
>>
>>     foreach (XmlAttribute a in v.Attributes)
>>     {
>>       if (a.Name.ToUpper() == tcAttribute.ToUpper())
>>         return true;
>>     }
>>      return false;
>>    }
>>Why do you believe .Clone is needed ?
>>But you're right in that the .RemoveAll is not neccessary.....
>
>
>Clone is needed because he saves the last node used in oXmlAttributeCollection and
>at the beginning of the method he writes oXmlAttributeCollection.RemoveAll()
>
>So, instead of just clearing the reference ( setting it to null or Nothing), he uses RemoveAll() on the saved reference. If you work like that you need cloning
>
>It's not clear to me why he needs oXmlAttributeCollection in the first place. But, like you said, it's difficult without knowing the whole picture

Would oXmlAttributeCollection only be relevant if it was a reference to the attribute collections of tcNode itself ?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform