Results 1 to 6 of 6

Thread: Can someone explain this code ??

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Can someone explain this code ??

    [2005]
    I have been asked to amend someone elses code and am not sure about it.
    Can someone explain this this to me :

    Code:
    Dim xmlSearchDoc As New XmlDocument()
    xmlSearchDoc.LoadXml(sb.ToString())
    Dim nameMan As New XmlNamespaceManager(xSearchDoc.NameTable)
    nameMan.AddNamespace("nameMan", xmlSearchDoc.DocumentElement.NamespaceURI)
    XmlNodeList nlResults = xmlSearchDoc.SelectNodes(@"//namesMan:LocationName", nameMan)
    xmlSearchDoc contains the following :

    Code:
    <Organisations>
     <Organisation xmlns="http://www.test.com/LocationRecord">
      <GUID>{0378BE2E-D3A5-4905-AA80-4C3138B6726E}</GUID> 
      <LocationName>Location Test</LocationName>
    </Organisation>
    </Organisations>
    Firstly, I dont understand the namespacemanager bit. Why add a namespacemanager ?
    And why is it that I return nothing when I do my "XmlNodeList nlResults = xmlSearchDoc.SelectNodes(@"//namesMan:LocationName", nameMan)"
    Last edited by venerable bede; Apr 17th, 2008 at 05:35 AM.

    Parksie

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Can someone explain this code ??

    I think you have to save your doc first to get the new node.

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Can someone explain this code ??

    I'm pretty certain that ain't the problem.

    Parksie

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can someone explain this code ??

    sapator - saving the file is not necessary.

    vb - Let me see if I can break it down...
    Code:
    Dim xmlSearchDoc As New XmlDocument() 'Pretty sure I don't need to explain this
    xmlSearchDoc.LoadXml(sb.ToString()) 'Or this... moving on...
    
    'This creates a namespace manager, used for easily managing namespace throughout the document
    Dim nameMan As New XmlNamespaceManager(xSearchDoc.NameTable) 
    
    'Here, it's adding a namespace to the document - but for some reason, it's using the default namespace URI... ??? WTH?
    nameMan.AddNamespace("nameMan", xmlSearchDoc.DocumentElement.NamespaceURI) 
    'If you were to save the document at this point, it would look like this: nameMan:xmlns="http://www.test.com/LocationRecord"
    'Which is pretty retarded if you ask me since the document already has a default namespace 
    
    'This line is supposed to find elements under the nameMan namespace that are called LocationName
    XmlNodeList nlResults = xmlSearchDoc.SelectNodes(@"//namesMan:LocationName", nameMan)
    I think the reason it doesn't work is because the addition of the junk (nameMan) namespace. When adding the namespace, since there is already a default one, it doesn't know to internally change LocationName to nameMan:LocationName .... I'm not sure what the developer thought they were doing.

    Here's what I'd do...
    Remove these lines
    Dim nameMan As New XmlNamespaceManager(xSearchDoc.NameTable)
    nameMan.AddNamespace("nameMan", xmlSearchDoc.DocumentElement.NamespaceURI)

    And change the search to just LocationName....
    So it would look like this:


    Code:
    Dim xmlSearchDoc As New XmlDocument()
    xmlSearchDoc.LoadXml(sb.ToString())
    XmlNodeList nlResults = xmlSearchDoc.SelectNodes(@"//namesMan:LocationName")
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Can someone explain this code ??

    Thanks techgnome.

    I'm still not 100% on this but you have made it a lot clearer.

    Parksie

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Can someone explain this code ??

    And you're mixing VB.Net code with C# code too.
    Code:
    'This indicate that the code is written in VB.net
    Dim xmlSearchDoc As New XmlDocument()
    
    'Then this line is using C# syntax
    XmlNodeList nlResults = xmlSearchDoc.SelectNodes(@"//namesMan:LocationName", nameMan)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width