Namespace prefix and VB XML axis properties
-
jueves, 19 de abril de 2012 2:43
I just want to verify a conceptual issue.
This code is given in a tutorial at http://msdn.microsoft.com/en-us/library/bb943911.aspx
Public Function ParagraphText(ByVal e As XElement) As String Dim w = e.Name.Namespace Return (e.<w:r>.<w:t>).StringConcatenate(Function(element) CStr(element)) End FunctionBut you cannot use namespace prefixes like this in VB axis properties can you? This example only happens to work because the code file
Imports <xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
That is, the fortuitous choice of "w" as the temp namespace variable collides with the imported global namespace prefix, and that's the only reason the "e.<w:r>..." syntax is valid?
I'm interested because I'd like to write a method that can access child elements of an XElement using sytnax that ensures they are in the namespace of the parent (not directly available in the called context).
I can do it with explicit LINQ methods, but not axis properties (not sure if terminology is correct. The distinction I'm trying to make is clear in the code below between the commented and uncommented regions). Here is my attempt:
Public Sub InitFromXElement(xel As XElement) Dim w As XNamespace = xel.Name.Namespace Debug.Print("Namespace of xel=" & w.ToString()) 'Me.Name = xel.<w:Name>(0).Value 'Me.Field = xel.<w:Field>(0).Value 'Me.Description = xel.<w:Description>(0).Value 'Me.Unit = xel.<w:Unit>(0).Value Me.Name = xel.Element(w + "Name").Value Me.Field = xel.Element(w + "Field").Value Me.Description = xel.Element(w + "Description").Value Me.Unit = xel.Element(w + "Unit").Value End Sub
- Editado RokShox jueves, 19 de abril de 2012 3:02 highlight problem in code
Todas las respuestas
-
jueves, 19 de abril de 2012 9:48
I agree with you, prefixes used in VB.NET axis properties need to be defined globally with "Imports" directives as the VB.NET compiler translates the code into normal axis methods calls at compile time.
Thus if you want to use XNamespace objects you need to use the axis methods like e.g. foo.Elements(w + "Name") or foo.Descendants(w + "Name").
MVP Data Platform Development My blog
- Marcado como respuesta RokShox viernes, 20 de abril de 2012 1:48

