XML Namespaces

  • Thread starter Thread starter Lou
  • Start date Start date
L

Lou

I have been working on something very simple for days now argh!
In VB6 it was 4 lines of code in 5 minutes!
In VB .NET how do i parse this simple xml documenet.
I cant get a reference to any of the nodes.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <cap:alert xmlns:cap="http://www.incident.com/cap/1.0">
<cap:identifier>NOAA-NWS-ALERTS Alabama
2007-02-16T10:03:56-05:00</cap:identifier>
<cap:sender>[email protected]</cap:sender>
<cap:sent>2007-02-16T10:03:56-05:00</cap:sent>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:scope>Public</cap:scope>
<cap:note>Current Watches, Warnings and Advisories for Alabama Issued by
the National Weather Service</cap:note>
</cap:alert>
 
Lou said:
In VB .NET how do i parse this simple xml documenet.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <cap:alert xmlns:cap="http://www.incident.com/cap/1.0">
<cap:identifier>NOAA-NWS-ALERTS Alabama
2007-02-16T10:03:56-05:00</cap:identifier>
<cap:sender>[email protected]</cap:sender>
<cap:sent>2007-02-16T10:03:56-05:00</cap:sent>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:scope>Public</cap:scope>
<cap:note>Current Watches, Warnings and Advisories for Alabama Issued by
the National Weather Service</cap:note>
</cap:alert>

..NET has several ways to parse XML, one is XPathDocument/XPathNavigator

Imports System
Imports System.Xml
Imports System.Xml.XPath

Module Module1

Sub Main()
Dim Navigator As XPathNavigator = New
XPathDocument("XMLFile1.xml").CreateNavigator()
Dim NodeIterator As XPathNodeIterator = Navigator.Select("*/*")
While NodeIterator.MoveNext()
Console.WriteLine("Element name: ""{0}"", content:
""{1}""", NodeIterator.Current.LocalName, NodeIterator.Current.Value)
End While
End Sub

End Module
 
I have been working on something very simple for days now argh!
In VB6 it was 4 lines of code in 5 minutes!
In VB .NET how do i parse this simple xml documenet.
I cant get a reference to any of the nodes.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <cap:alert xmlns:cap="http://www.incident.com/cap/1.0">
<cap:identifier>NOAA-NWS-ALERTS Alabama
2007-02-16T10:03:56-05:00</cap:identifier>
<cap:sender>[email protected]</cap:sender>
<cap:sent>2007-02-16T10:03:56-05:00</cap:sent>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:scope>Public</cap:scope>
<cap:note>Current Watches, Warnings and Advisories for Alabama Issued by
the National Weather Service</cap:note>
</cap:alert>

This would be a quick way to grab those individual node values of the
alert. If you want references to the actual nodes then change id,
sent, etc. variables to the type XmlNode and drop the .InnerXml in the
lines that assign the values.

Private Sub DoXmlStuff()

Dim x As New XmlDocument
Dim ns As XmlNamespaceManager
Dim id As String
Dim sent As String
Dim status As String
Dim msgType As String
Dim scope As String
Dim note As String

' Load the document
x.Load("whereverMyFileIs.xml")
' Create the NameSpaceManager
ns = New XmlNamespaceManager(x.NameTable)
ns.AddNamespace("cap", "http://www.incident.com/cap/1.0")
id = x.SelectSingleNode("//cap:alert", ns).InnerXml
sent = x.SelectSingleNode("//cap:sent", ns).InnerXml
status = x.SelectSingleNode("//cap:status", ns).InnerXml
msgType = x.SelectSingleNode("//cap:msgType", ns).InnerXml
scope = x.SelectSingleNode("//cap:scope", ns).InnerXml
note = x.SelectSingleNode("//cap:note", ns).InnerXml

Dim sOutput As String = String.Format("ID: {0} Sent: {1}
Status: {2} MsgType: {3} Scope: {4} Note: {5}", id, sent, status,
msgType, scope, note)

MsgBox(sOutput)

End Sub
 
Back
Top