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)"
Re: Can someone explain this code ??
I think you have to save your doc first to get the new node.
Re: Can someone explain this code ??
I'm pretty certain that ain't the problem.
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
Re: Can someone explain this code ??
Thanks techgnome.
I'm still not 100% on this but you have made it a lot clearer.
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)