How to read the specified node value from XML

  • Thread starter Thread starter YXQ
  • Start date Start date
Y

YXQ

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>
 
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.
 
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
 
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 said:
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 said:
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>
 
YXQ said:
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.
 
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 said:
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 said:
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 said:
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>
 
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 said:
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 said:
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 said:
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

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>
 
And "//A/C/resource[@name = 'CCC']" is right also, thank you.

Stanimir Stoyanov said:
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 said:
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 said:
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

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>
 
Stanimir said:
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.
 
Back
Top