473,614 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14934
On Nov 3, 8:40*am, YXQ <Y...@discussio ns.microsoft.co mwrote:
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("MyFi le.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSing leNode("//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*@discussion s.microsoft.com wrote in message
news:0D******** *************** ***********@mic rosoft.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.SelectSing leNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(M yFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader )
Dim root As XmlNode = doc.DocumentEle ment
Dim node As XmlNode = root.SelectSing leNode("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("MyFi le.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSing leNode("//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*@discussion s.microsoft.com wrote in message
news:0D******** *************** ***********@mic rosoft.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.SelectSing leNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(M yFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader )
Dim root As XmlNode = doc.DocumentEle ment
Dim node As XmlNode = root.SelectSing leNode("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 SelectSingleNod e 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*@discussion s.microsoft.com wrote in message
news:66******** *************** ***********@mic rosoft.com...
The code is error
Dim xNode As xDoc.SelectSing leNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(M yFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader )
Dim root As XmlNode = doc.DocumentEle ment
Dim node As XmlNode = root.SelectSing leNode("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("MyFi le.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSing leNode("//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*@discussion s.microsoft.com wrote in message
news:0D******* *************** ************@mi crosoft.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*@discussion s.microsoft.com wrote in message
news:66******** *************** ***********@mic rosoft.com...
The code is error
Dim xNode As xDoc.SelectSing leNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(M yFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader )
Dim root As XmlNode = doc.DocumentEle ment
Dim node As XmlNode = root.SelectSing leNode("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("MyFi le.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSing leNode("//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*@discussion s.microsoft.com wrote in message
news:0D******** *************** ***********@mic rosoft.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*@discussion s.microsoft.com wrote in message
news:66******** *************** ***********@mic rosoft.com...
The code is error
Dim xNode As xDoc.SelectSing leNode("//resource[@name='" & resourceName &
"']")

My code has problem:
//////////////////////////////////////////////////////////
Dim reader As XmlReader = New XmlTextReader(M yFile.xml)
Dim doc As XmlDocument = New XmlDocument()
doc.Load(reader )
Dim root As XmlNode = doc.DocumentEle ment
Dim node As XmlNode = root.SelectSing leNode("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("MyFi le.xml")

Dim resourceName As String = "AAA"
Dim xNode As xDoc.SelectSing leNode("//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*@discussion s.microsoft.com wrote in message
news:0D******** *************** ***********@mic rosoft.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.SelectSing leNode( 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
6817
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
9
1324
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 grab both values and I can't seem to figure it out, just a bigginer. I'm able to grab aTime but can't get the aType value. Should I create a second routine or is there something I can stick in this to make it work for me. I just want those two...
1
4491
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 code="Ms">Ms</value> <value code="Miss">Miss</value>
0
1415
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 in advance When I run the pgm , I get the error: Can't parse login information. Object Reference not set to an instance of an object. I have added the Try-catch in all my functions. In ParseLoginResponse function, it catches the above...
9
5200
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 facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
4
7885
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: <Interface> <Product> <CategoryFeatureGroup ID="622" No="1"> <FeatureGroup ID="0">
6
7263
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 : evry day, i store the number of children in my classroom in my XML file. For exemple, on monday, my app ask me something like this : msgbox ("Are the 28 children here today ?",vbyesno)
2
11003
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 on a W23 server SP2, I get the following error: System.Runtime.Remoting.RemotingException: Remoting configuration failed with the exception 'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation....
0
1725
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 below: Sub readfile() objxmldom.async = False objxmldom.Load ("D:\CRT\rules\AQUA.xml")
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8279
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7093
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6088
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5540
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.