parsing a string as XML using VB.NET

W

wk6pack

Hi,

I would like to know how to parse the following xml string using vb.net?
currxml="
<employee><address_2></address_2><assignments><assignment><assigned_bargaini
ng_unit></assigned_bargaining_unit><assigned_department></assigned_departmen
t><assigned_locations><assigned_location></assigned_location></assigned_loca
tions><assigned_position></assigned_position><assigned_position_department><
/assigned_position_department></assignment></assignments><category>TEAC</cat
egory><city>VICTORIA</city><country_code>CAN</country_code><demographic_barg
aining_unit></demographic_bargaining_unit><demographic_department></demograp
hic_department> said:
<fax_no></fax_no><first_name>MURIEL</first_name><last_name>ANDREWS</last_na
me><middle_name></middle_name><most_assigned_bargaining_unit></most_assigned
_bargaining_unit><most_assigned_department></most_assigned_department><most_
assigned_location></most_assigned_location><most_assigned_position></most_as
signed_position><most_assigned_position_department></most_assigned_position_
department><postal_code>V8S
5G5</postal_code><preferred_name></preferred_name><province_code>BC</provinc
e_code><salutation>MS</salutation><sin>706257185</sin><status>T</status><str
eet>1050 DEAL STREET</street><telephone_no></telephone_no></employee>"

here is my code so far:
Dim strreader = New StringReader(currxml)

Dim xmldoc As New XmlDocument

xmldoc.LoadXml(currxml)

reader = New XmlNodeReader(xmldoc)

While reader.Read

Select Case reader.NodeType

Case XmlNodeType.Element

If reader.Name = "category" Then

MsgBox(reader.Name)

End If

End Select

End While



I do see the read.Name as category but it doesnt give me "TEAC". I dont get
any value in the msgbox. It is blank. Am I missing something?

I see the value for city which is "VICTORIA" but the reader.Name is "".

thanks for you help,

Will
 
W

wk6pack

Thanks Cor, Jay and M. Posseth. I now understand it a bit better.

Will


Jay B. Harlow said:
wk6pack,
| I would like to know how to parse the following xml string using vb.net?
I would "parse" it with either a XmlTextReader, XmlDocument, or
XPathDocument, something like:

Dim currxml As String = ...

Dim input As New StringReader(currxml)

Dim reader As New XmlTextReader(input)

If I loaded the string into an XmlDocument or an XPathDocument, I would use
the various methods on those objects (such as Select) to extract the content
that I need. Rarely would I "hand it back" to an XmlNodeReader...

| I do see the read.Name as category but it doesnt give me "TEAC". I dont
get
| any value in the msgbox. It is blank. Am I missing something?

Yes, you are confusing an Element Node with a Text Node. "<category>" is an
Element node, while "TEAC" is a Text Node which happens to be the contents
of the "<category>" Element node

| I see the value for city which is "VICTORIA" but the reader.Name is "".
Again, "VICTORIA" is a Text Node, which happens to be the contents of a
"<category>" Element node.

To see the different between the various node types, try:

Dim currxml As String = ...
Dim input As New StringReader(currxml)
Dim reader As New XmlTextReader(input)

While reader.Read
Debug.WriteLine(reader.NodeType, "node type")
Debug.WriteLine(reader.Prefix, "prefix")
Debug.WriteLine(reader.LocalName, "local name")
Debug.WriteLine(reader.Name, "name")
Debug.WriteLine(reader.NamespaceURI, "namespace")
Debug.WriteLine(reader.Value, "value")
Debug.WriteLine(Nothing)
End While

Hope this helps
Jay


| Hi,
|
| I would like to know how to parse the following xml string using vb.net?
| currxml="
|
ng_unit> said:
t> said:
tions> said:
/assigned_position_department> said:
egory> said:
aining_unit> said:
hic_department> said:
ition> said:
|

<fax_no></fax_no><first_name>MURIEL</first_name><last_name>ANDREWS</last_na
|
me> said:
_bargaining_unit> said:
assigned_location> said:
signed_position> said:
| department><postal_code>V8S
|
 
W

wk6pack

thanks Cor, M. Posseth and Jay.

Will


Jay B. Harlow said:
wk6pack,
| I would like to know how to parse the following xml string using vb.net?
I would "parse" it with either a XmlTextReader, XmlDocument, or
XPathDocument, something like:

Dim currxml As String = ...

Dim input As New StringReader(currxml)

Dim reader As New XmlTextReader(input)

If I loaded the string into an XmlDocument or an XPathDocument, I would use
the various methods on those objects (such as Select) to extract the content
that I need. Rarely would I "hand it back" to an XmlNodeReader...

| I do see the read.Name as category but it doesnt give me "TEAC". I dont
get
| any value in the msgbox. It is blank. Am I missing something?

Yes, you are confusing an Element Node with a Text Node. "<category>" is an
Element node, while "TEAC" is a Text Node which happens to be the contents
of the "<category>" Element node

| I see the value for city which is "VICTORIA" but the reader.Name is "".
Again, "VICTORIA" is a Text Node, which happens to be the contents of a
"<category>" Element node.

To see the different between the various node types, try:

Dim currxml As String = ...
Dim input As New StringReader(currxml)
Dim reader As New XmlTextReader(input)

While reader.Read
Debug.WriteLine(reader.NodeType, "node type")
Debug.WriteLine(reader.Prefix, "prefix")
Debug.WriteLine(reader.LocalName, "local name")
Debug.WriteLine(reader.Name, "name")
Debug.WriteLine(reader.NamespaceURI, "namespace")
Debug.WriteLine(reader.Value, "value")
Debug.WriteLine(Nothing)
End While

Hope this helps
Jay


| Hi,
|
| I would like to know how to parse the following xml string using vb.net?
| currxml="
|
ng_unit> said:
t> said:
tions> said:
/assigned_position_department> said:
egory> said:
aining_unit> said:
hic_department> said:
ition> said:
|

<fax_no></fax_no><first_name>MURIEL</first_name><last_name>ANDREWS</last_na
|
me> said:
_bargaining_unit> said:
assigned_location> said:
signed_position> said:
| department><postal_code>V8S
|
 
Top