get imported namespaces of a schema and their schema location


''' <summary>
''' get all imported namespaces of a schema and their schema location
''' </summary>
''' <param name="oSchema">schmema</param>
''' <returns>Dictionary(imported namespace, schema location) </returns>
''' <remarks></remarks>
Function RecurseExternalSchemas(ByVal oSchema As XmlSchema) As Dictionary(Of String, String)
Dim oResult As New Dictionary(Of String, String)
For Each oExternal As XmlSchemaExternal In oSchema.Includes
If oExternal.GetType() Is GetType(XmlSchemaImport) Then
Dim import As XmlSchemaImport = CType(oExternal, XmlSchemaImport)
If Not oExternal.SchemaLocation = Nothing Then
oResult.Add(import.Namespace, oExternal.SchemaLocation)
Else
oResult.Add(import.Namespace, String.Empty)
End If
End If
If Not oExternal.Schema Is Nothing Then
RecurseExternalSchemas(oExternal.Schema)
End If
Next
RecurseExternalSchemas = oResult
End Function

..:: Whereever you go, stay in touch. Download toolbar now! It´s free, private and secure. ::..

Advertisements

a recursive walk through XPathNavigator

Sub RecursiveWalkThroughXpath(ByVal oNavigator As XPathNavigator)
Select Case oNavigator.NodeType
Case XPathNodeType.Element
If oNavigator.Prefix = String.Empty Then
Console.WriteLine(“<{0}>”, oNavigator.LocalName)
Else
Console.Write(“<{0}:{1}>”, oNavigator.Prefix, oNavigator.LocalName)
Console.WriteLine(vbTab + oNavigator.NamespaceURI)
End If
Case XPathNodeType.Text
Console.WriteLine(vbTab + oNavigator.Value)
End Select

If oNavigator.MoveToFirstChild() Then
Do
RecursiveWalkThroughXpath(oNavigator)
Loop While (oNavigator.MoveToNext())

oNavigator.MoveToParent()
If (oNavigator.NodeType = XPathNodeType.Element) Then
Console.WriteLine(“</{0}>”, oNavigator.Name)
End If
Else
If oNavigator.NodeType = XPathNodeType.Element Then
Console.WriteLine(“</{0}>”, oNavigator.Name)
End If
End If
End Sub

..:: Whereever you go, stay in touch. Download toolbar now! It´s free, private and secure. ::..

CLR stored procedure to create a external schema (xsd) or a xml file including schema from a SQL Server table

<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub spLBgenerateInstallscript( _
ByVal sTablename As String, _
<Out()> ByRef iReturnvalue As SqlInt32)

Dim oCommand As SqlCommand
Dim oDom As New Xml.XmlDocument
Dim oDom2 As New Xml.XmlDocument
Dim oReader As Xml.XmlReader
Dim sXML As String = String.Empty
Dim oSB As New StringBuilder
Dim sOutputfilename As String

Using oConnection As New SqlConnection(“context connection=true”)
Try

oConnection.Open()
oCommand = oConnection.CreateCommand
oCommand.CommandText = “SELECT * FROM ” & sTablename & ” FOR XML AUTO, TYPE, XMLSCHEMA, ELEMENTS, ROOT (‘” & sTablename & “‘)”
oCommand.CommandType = CommandType.Text
oReader = oCommand.ExecuteXmlReader

oReader.Read()
Do While oReader.ReadState <> Xml.ReadState.EndOfFile
sXML &= oReader.ReadOuterXml()
Loop
oReader.Close()
oDom.LoadXml(sXML)

Dim schema As Xml.XmlNode = oDom.ChildNodes(0).FirstChild

Dim mynode As Xml.XmlNode = oDom2.ImportNode(schema, True)
oDom2.AppendChild(mynode)

‘write schema only
sOutputfilename = “C:\” & sTablename & “_” & String.Format(“{0:” & “yyyyMMddHHmmss” & “}”, Now)
oDom2.Save(sOutputfilename & “.xsd”)

‘write xml with inline schema
oDom.Save(sOutputfilename & “.xml”)

iReturnvalue = 0

Catch SQLex As Exception

#If DEBUG Then
EmitDebugMessage(“spLBgenerateInstallscript exeption SQLex: ” & SQLex.Message)
#End If
iReturnvalue = 1

Finally

If oConnection.State <> ConnectionState.Closed Then
oConnection.Close()
End If

End Try
End Using

End Sub

..:: Whereever you go, stay in touch. Download toolbar now! It´s free, private and secure. ::..

sample XmlReader Validation Handler

”’ <summary>
”’ XmlReader Validation Handler
”’ </summary>
”’ <param name=”sender”></param>
”’ <param name=”e”></param>
”’ <remarks>not used, not ready</remarks>
Sub oXmlReaderSettingsValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
If e.Severity = XmlSeverityType.Warning Then
Console.Write(“WARNING: “)
Console.WriteLine(e.Message)
ElseIf e.Severity = XmlSeverityType.Error Then
Console.Write(“ERROR: “)
Console.WriteLine(e.Message)
End If
End Sub

..:: Whereever you go, stay in touch. Download toolbar now! It´s free, private and secure. ::..

validate XML File against inline Schema

”’ <summary>
”’ validates XML File against inline Schema
”’ </summary>
”’ <param name=”sXmlFile”>xml File mit Schema und Daten</param>
”’ <returns>true no errors, false errors</returns>
”’ <remarks>not used, not ready</remarks>
”’
Public Function ValidateXMLFileInternalXSD(ByVal sXmlFile As String) As Boolean
Try
Dim oXmlReaderSettings As New XmlReaderSettings()
oXmlReaderSettings.ValidationType = ValidationType.Schema
oXmlReaderSettings.ValidationFlags = oXmlReaderSettings.ValidationFlags Or XmlSchemaValidationFlags.ProcessInlineSchema
oXmlReaderSettings.ValidationFlags = oXmlReaderSettings.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler oXmlReaderSettings.ValidationEventHandler, AddressOf oXmlReaderSettingsValidationEventHandler
Dim reader As XmlReader = XmlReader.Create(sXmlFile, oXmlReaderSettings)
‘ parse reader
While (reader.Read())
End While
Catch ex As XmlException
Console.WriteLine(“XmlDocumentValidationExample.XmlException: {0}”, ex.Message)
Return False
Catch ex As XmlSchemaValidationException
Console.WriteLine(“XmlDocumentValidationExample.XmlSchemaValidationException: {0}”, ex.Message)
Return False
Catch ex As Exception
Console.WriteLine(“XmlDocumentValidationExample.Exception: {0}”, ex.Message)
Return False
End Try
Return True
End Function

..:: Whereever you go, stay in touch. Download toolbar now! It´s free, private and secure. ::..

validate XML File against XSD File

”’ <summary>
”’ validates XML File against XSD File
”’ </summary>
”’ <param name=”sXmlFile”>xml data</param>
”’ <param name=”sXSDFile”>external xml schema</param>
”’ <returns>true no errors, false errors</returns>
”’ <remarks></remarks>
Public Function ValidateXMLFileExternalXSD(ByVal sXmlFile As String, ByVal sXSDFile As String)
Try
Dim oXmlReaderSettings As XmlReaderSettings = New XmlReaderSettings()
oXmlReaderSettings.Schemas.Add(“http://www.tempuri.org&#8221;, sXSDFile)
AddHandler oXmlReaderSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf oXmlReaderSettingsValidationEventHandler)
oXmlReaderSettings.ValidationFlags = oXmlReaderSettings.ValidationFlags And XmlSchemaValidationFlags.ReportValidationWarnings
oXmlReaderSettings.ValidationType = ValidationType.Schema
Dim reader As XmlReader = XmlReader.Create(sXmlFile, oXmlReaderSettings)
Dim oDom As New XmlDocument
oDom.Load(reader)
Catch ex As XmlException
Console.WriteLine(“XmlDocumentValidationExample.XmlException: {0}”, ex.Message)
Return False
Catch ex As XmlSchemaValidationException
Console.WriteLine(“XmlDocumentValidationExample.XmlSchemaValidationException: {0}”, ex.Message)
Return False
Catch ex As Exception
Console.WriteLine(“XmlDocumentValidationExample.Exception: {0}”, ex.Message)
Return False
Finally

End Try
Return True
End Function

..:: Whereever you go, stay in touch. Download toolbar now! It´s free, private and secure. ::..

get namespaces from xml with help of XPathNavigator

Dim sXmlFragment As String = oDom.FirstChild.ChildNodes(0).OuterXml
Dim oXDom As New XPathDocument(New StringReader(sXmlFragment))
Dim oXPN As XPathNavigator = oXDom.CreateNavigator()
oXPN.MoveToFollowing(XPathNodeType.Element)
Dim oNamespaces As IDictionary(Of String, String) = oXPN.GetNamespacesInScope(XmlNamespaceScope.All)
Dim sSchemaNamespace As String = String.Empty
For Each de As KeyValuePair(Of String, String) In oNamespaces
oNsmgr.AddNamespace(de.Key, de.Value)
Next

..:: Whereever you go, stay in touch. Download toolbar now! It´s free, private and secure. ::..