473,395 Members | 1,637 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,395 software developers and data experts.

How to read the specified node value from XML

I want to do the multi-language program, save the language text in XML file,
but how to read the specified node value? the xml is below, for example, i
want to get the value(AAA content) that named "AAA" or the value(CCC content)
that named "CCC".
/////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<A>
<resource name="AAA">AAA content</resource>
<B>
<resource name="BBB">BBB content</resource>
<resource name="CCC">CCC content</resource>
<resource name="DDD">DDD content</resource>
</B>
</A>
Nov 3 '08 #1
8 14909
On Nov 3, 8:40*am, YXQ <Y...@discussions.microsoft.comwrote:
I want to do the multi-language program, save the language text in XML file,
but how to read the specified node value? the xml is below, for example, i
want to get the value(AAA content) that named "AAA" or the value(CCC content)
that named "CCC".
/////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<A>
* * <resource name="AAA">AAA content</resource>
* * <B>
* * * * <resource name="BBB">BBB content</resource>
* * * * <resource name="CCC">CCC content</resource>
* * * * <resource name="DDD">DDD content</resource>
* * </B>
</A>
One way is to use the DOM. Read the entire XML file into an
XmlDocument object. Use the SelectNodes method to retrieve the
resource names as an array of nodes. Loop through the nodes to get
what you want from the node's Inner_Text property.
Nov 3 '08 #2
XPath expressions are useful in this case. You can encapsulate the following
code in a function for easier access (multiple resource-specific strings):

Imports System.Xml
' ...
Dim xDoc As New XmlDocument()
xDoc.Load("MyFile.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" &
resourceName & "']")
Dim value As String = String.Empty

' Ensure that we found a matching node
If xNode IsNot Nothing
Return xNode.InnerText
End If
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
>I want to do the multi-language program, save the language text in XML
file,
but how to read the specified node value? the xml is below, for example, i
want to get the value(AAA content) that named "AAA" or the value(CCC
content)
that named "CCC".
/////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<A>
<resource name="AAA">AAA content</resource>
<B>
<resource name="BBB">BBB content</resource>
<resource name="CCC">CCC content</resource>
<resource name="DDD">DDD content</resource>
</B>
</A>
Nov 3 '08 #3
The code is error
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(MyFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader)
Dim root As XmlNode = doc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode("A/B/[@resource
name='CCC']") ' this line has problem...............................

Dim result As String = ""
If node IsNot Nothing Then
result = node.InnerText
return result
End If
reader.Close()
"Stanimir Stoyanov" wrote:
XPath expressions are useful in this case. You can encapsulate the following
code in a function for easier access (multiple resource-specific strings):

Imports System.Xml
' ...
Dim xDoc As New XmlDocument()
xDoc.Load("MyFile.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" &
resourceName & "']")
Dim value As String = String.Empty

' Ensure that we found a matching node
If xNode IsNot Nothing
Return xNode.InnerText
End If
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
I want to do the multi-language program, save the language text in XML
file,
but how to read the specified node value? the xml is below, for example, i
want to get the value(AAA content) that named "AAA" or the value(CCC
content)
that named "CCC".
/////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<A>
<resource name="AAA">AAA content</resource>
<B>
<resource name="BBB">BBB content</resource>
<resource name="CCC">CCC content</resource>
<resource name="DDD">DDD content</resource>
</B>
</A>
Nov 3 '08 #4
YXQ wrote:
The code is error
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(MyFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader)
Dim root As XmlNode = doc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode("A/B/[@resource
name='CCC']") ' this line has problem...............................
The correct XPath syntax is
"A/B/resource[@name = 'CCC']"
but then you need to call SelectSingleNode on the doc variable, not on
the DocumentElement.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 3 '08 #5
Why are you using "A/B/[@resource name='CCC']"? It is not a correct XPath
expression.

You should use "//resource[@name='YOUR_KEY_HERE']" instead. Exclude A/B/,
because of the two forward slashes XPath will select any 'resource' node
whose 'name' attribute is the 'key', despite of the node hierarchy.
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:66**********************************@microsof t.com...
The code is error
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(MyFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader)
Dim root As XmlNode = doc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode("A/B/[@resource
name='CCC']") ' this line has problem...............................

Dim result As String = ""
If node IsNot Nothing Then
result = node.InnerText
return result
End If
reader.Close()
"Stanimir Stoyanov" wrote:
>XPath expressions are useful in this case. You can encapsulate the
following
code in a function for easier access (multiple resource-specific
strings):

Imports System.Xml
' ...
Dim xDoc As New XmlDocument()
xDoc.Load("MyFile.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" &
resourceName & "']")
Dim value As String = String.Empty

' Ensure that we found a matching node
If xNode IsNot Nothing
Return xNode.InnerText
End If
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:0D**********************************@microso ft.com...
>I want to do the multi-language program, save the language text in XML
file,
but how to read the specified node value? the xml is below, for
example, i
want to get the value(AAA content) that named "AAA" or the value(CCC
content)
that named "CCC".
/////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<A>
<resource name="AAA">AAA content</resource>
<B>
<resource name="BBB">BBB content</resource>
<resource name="CCC">CCC content</resource>
<resource name="DDD">DDD content</resource>
</B>
</A>
Nov 3 '08 #6
Yes, the "//resource[@name='YOUR_KEY_HERE']" is right!
But the speed of finding values is slow if there are lots of nodes? which
way is quick?
Thank you.

"Stanimir Stoyanov" wrote:
Why are you using "A/B/[@resource name='CCC']"? It is not a correct XPath
expression.

You should use "//resource[@name='YOUR_KEY_HERE']" instead. Exclude A/B/,
because of the two forward slashes XPath will select any 'resource' node
whose 'name' attribute is the 'key', despite of the node hierarchy.
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:66**********************************@microsof t.com...
The code is error
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(MyFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader)
Dim root As XmlNode = doc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode("A/B/[@resource
name='CCC']") ' this line has problem...............................

Dim result As String = ""
If node IsNot Nothing Then
result = node.InnerText
return result
End If
reader.Close()
"Stanimir Stoyanov" wrote:
XPath expressions are useful in this case. You can encapsulate the
following
code in a function for easier access (multiple resource-specific
strings):

Imports System.Xml
' ...
Dim xDoc As New XmlDocument()
xDoc.Load("MyFile.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" &
resourceName & "']")
Dim value As String = String.Empty

' Ensure that we found a matching node
If xNode IsNot Nothing
Return xNode.InnerText
End If
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
I want to do the multi-language program, save the language text in XML
file,
but how to read the specified node value? the xml is below, for
example, i
want to get the value(AAA content) that named "AAA" or the value(CCC
content)
that named "CCC".
/////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<A>
<resource name="AAA">AAA content</resource>
<B>
<resource name="BBB">BBB content</resource>
<resource name="CCC">CCC content</resource>
<resource name="DDD">DDD content</resource>
</B>
</A>

Nov 3 '08 #7
And "//A/C/resource[@name = 'CCC']" is right also, thank you.

"Stanimir Stoyanov" wrote:
Why are you using "A/B/[@resource name='CCC']"? It is not a correct XPath
expression.

You should use "//resource[@name='YOUR_KEY_HERE']" instead. Exclude A/B/,
because of the two forward slashes XPath will select any 'resource' node
whose 'name' attribute is the 'key', despite of the node hierarchy.
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:66**********************************@microsof t.com...
The code is error
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(MyFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader)
Dim root As XmlNode = doc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode("A/B/[@resource
name='CCC']") ' this line has problem...............................

Dim result As String = ""
If node IsNot Nothing Then
result = node.InnerText
return result
End If
reader.Close()
"Stanimir Stoyanov" wrote:
XPath expressions are useful in this case. You can encapsulate the
following
code in a function for easier access (multiple resource-specific
strings):

Imports System.Xml
' ...
Dim xDoc As New XmlDocument()
xDoc.Load("MyFile.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSingleNode("//resource[@name='" &
resourceName & "']")
Dim value As String = String.Empty

' Ensure that we found a matching node
If xNode IsNot Nothing
Return xNode.InnerText
End If
--
Stanimir Stoyanov
http://stoyanoff.info

"YXQ" <YX*@discussions.microsoft.comwrote in message
news:0D**********************************@microsof t.com...
I want to do the multi-language program, save the language text in XML
file,
but how to read the specified node value? the xml is below, for
example, i
want to get the value(AAA content) that named "AAA" or the value(CCC
content)
that named "CCC".
/////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<A>
<resource name="AAA">AAA content</resource>
<B>
<resource name="BBB">BBB content</resource>
<resource name="CCC">CCC content</resource>
<resource name="DDD">DDD content</resource>
</B>
</A>

Nov 3 '08 #8
Stanimir Stoyanov wrote:
Why are you using "A/B/[@resource name='CCC']"? It is not a correct
XPath expression.
Agreed.
You should use "//resource[@name='YOUR_KEY_HERE']" instead.
I have to disagree ...
Exclude A/B/, because of the two forward slashes XPath will select any
'resource' node whose 'name' attribute is the 'key', despite of the node
hierarchy.
Which
(a) assumes that keys will be unique, which I doubt they will be, and
(b) will become /interminably/ slow as the file grows in size.

/If/ the file has a meaningful structure, then why not make /use/ of it
to speed things up?

Dim sXPath as String = " A / B / resource[ @name = 'CCC' ] ")
Dim node as XmlNode = root.SelectSingleNode( sXPath )

If Not ( node Is Nothing ) Then
. . .
HTH,
Phill W.
Nov 4 '08 #9

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
9
by: B-Dog | last post by:
I'm trying to read two nodes in an xml file that I'm using to store setting but I'm having a hard time trying to read the values of two nodes. Below I can read one node but I need to be able to...
1
by: David | last post by:
All I am using the code below in order to read an XML file in the format: <root> <selection-attr attr="p70"> <value code="Mr">Mr</value> <value code="Mrs">Mrs</value> <value...
0
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
4
by: Pim75 | last post by:
Hello, I have to read a XML file in ASP and save the values in a database. I can get this work, but I cannot read some nested nodes of the xml file. This is a part of the XML file: ...
6
by: | last post by:
Hi, I'm steel trying to read and update my XML file with Visual Basic Express but i am unable to find the right way to read my xml file and update it if neccessary... Here is my problem :...
2
by: =?Utf-8?B?c2FtMDFt?= | last post by:
I have a remoting application that was developed on a Windows XP SP2 machine with VS2005 SP1. I finally got everything deployed using Wix 3.0, and it works great. Problem is, when I install the msi...
0
by: Kavitha Sudhershan | last post by:
hi, i wanna read the node values from xml. As per my code i can read the node values in first child node and for the next node am not able to read the node values. pls help me. i'll paste the code...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.