473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

parsing a string as XML using VB.NET

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_barga ini
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_departmen t></demograp
hic_department><demographic_location></demographic_location><demographic_pos
ition></demographic_position><email></email><employee_no>202307</employee_no
<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
Nov 21 '05 #1
7 18113
WK6pack,

When you are lucky than it has a dataset format. Add for that a dataset from
the items to your solution and paste your file in, than you see that direct.

When not you can better take the XMLNodeReader
http://msdn.microsoft.com/library/de...classtopic.asp

I take the xmldoc only in JavaScript.

I hope this helps,

Cor
Nov 21 '05 #2
Hi Cor,

I'm not sure what you mean by dataset format. But I am using the
XMLNodeReader.
I would like to know how do I know the tag has a value or not.

thanks,
Will
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OT**************@TK2MSFTNGP14.phx.gbl...
WK6pack,

When you are lucky than it has a dataset format. Add for that a dataset from the items to your solution and paste your file in, than you see that direct.
When not you can better take the XMLNodeReader
http://msdn.microsoft.com/library/de...classtopic.asp
I take the xmldoc only in JavaScript.

I hope this helps,

Cor

Nov 21 '05 #3
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
"wk6pack" <wk***@sd61.bc.ca> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
| 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_barga ini
|
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_departmen t></demograp
|
hic_department><demographic_location></demographic_location><demographic_pos
|
ition></demographic_position><email></email><employee_no>202307</employee_no
|
<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
|
|
Nov 21 '05 #4
Wk6Pack,

I checked it, your file is a normal dataset. When you open in the designer
as item an XML file and than paste your data in, than it will create for you
a dataset. When you than go to Data view below on the page, than you see a
kind of datagrid.

You can process this file as a normal dataset with
dim ds as new dataset
ds.readxml("myfile")

To get the values from by instance non strongly typed. (what you can
generate as well by right clicking on that datagrid and than do generate
dataset). However beneath non strongly typed. Than the first category from
employee is

Dim mystring as string = ds.tables("employee").Rows(0)("category")
'be aware that those columnames are case sensitive.

I hope this helps,

Cor

Nov 21 '05 #5
As extra tip to Cor`s thought about putting the data in a dataset

dim currxml="
<employee><address_2></address_2><assignments><assignment><assigned_barga ini
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_departmen t></demograp
hic_department><demographic_location></demographic_location><demographic_pos
ition></demographic_position><email></email><employee_no>202307</employee_no
<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>"
Dim xmldoc As New XmlDocument

xmldoc.LoadXml(currxml)

Dim XMLnr As New XmlNodeReader(Xmldoc.SelectSingleNode("employee"))

Dim ds As New DataSet

ds.ReadXml(XMLnr)

Dim ds As New DataSet

ds.ReadXml(XMLnr)

'to prove that it works throw a datagrid on the form

DataGrid1.DataSource = ds

happy coding :-)

M. Posseth [MCP Developer]
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OJ**************@TK2MSFTNGP15.phx.gbl... Wk6Pack,

I checked it, your file is a normal dataset. When you open in the designer
as item an XML file and than paste your data in, than it will create for you a dataset. When you than go to Data view below on the page, than you see a
kind of datagrid.

You can process this file as a normal dataset with
dim ds as new dataset
ds.readxml("myfile")

To get the values from by instance non strongly typed. (what you can
generate as well by right clicking on that datagrid and than do generate
dataset). However beneath non strongly typed. Than the first category from
employee is

Dim mystring as string = ds.tables("employee").Rows(0)("category")
'be aware that those columnames are case sensitive.

I hope this helps,

Cor

Nov 21 '05 #6
Thanks Cor, Jay and M. Posseth. I now understand it a bit better.

Will
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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
"wk6pack" <wk***@sd61.bc.ca> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
| 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_barga ini |
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_departmen t></demograp |
hic_department><demographic_location></demographic_location><demographic_pos |
ition></demographic_position><email></email><employee_no>202307</employee_no |

<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
|
|

Nov 21 '05 #7
thanks Cor, M. Posseth and Jay.

Will
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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
"wk6pack" <wk***@sd61.bc.ca> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
| 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_barga ini |
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_departmen t></demograp |
hic_department><demographic_location></demographic_location><demographic_pos |
ition></demographic_position><email></email><employee_no>202307</employee_no |

<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
|
|

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
6
by: BerkshireGuy | last post by:
Does anyone know of a good function that will parse out parts of an SQL statement that is passed to it in seperate variables? It should be able to parse statements that contain ORDERBY, WHERE,...
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
2
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important...
6
by: gw7rib | last post by:
I have a program that needs to do a small amount of relatively simple parsing. The routines I've written work fine, but the code using them is a bit long-winded. I therefore had the idea of...
1
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.