Hack Forums
[.NET] How to read text from xml nodes - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] How to read text from xml nodes (/showthread.php?tid=5189923)



How to read text from xml nodes - hunters - Mar 2, 2016

Hello HF,

This is a sample xml
Code
<Contents><Key>000018a19cfbbcb12286c4c051b09b27.jpeg</Key><LastModified>2016-02-10T23:58:24.000Z</LastModified><ETag>"000018a19cfbbcb12286c4c051b09b27"</ETag><Size>227621</Size><Owner><ID>07ba55931634353d81b28388db73b19c576cefcc3f5a19be3137e5cbf691f554</ID><DisplayName>admin</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>00002627b319131d0de89347c32ad3b6.jpeg</Key><LastModified>2016-02-13T12:34:54.000Z</LastModified><ETag>"00002627b319131d0de89347c32ad3b6"</ETag><Size>102336</Size><Owner><ID>07ba55931634353d81b28388db73b19c576cefcc3f5a19be3137e5cbf691f554</ID><DisplayName>admin</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>00002aee6026afbd407096d212d88344.jpg</Key><LastModified>2016-02-21T22:44:02.000Z</LastModified><ETag>"00002aee6026afbd407096d212d88344"</ETag><Size>69219</Size><Owner><ID>07ba55931634353d81b28388db73b19c576cefcc3f5a19be3137e5cbf691f554</ID><DisplayName>admin</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>00002e987b80919a095bf8da63d8399b.jpg</Key><LastModified>2016-02-22T15:02:14.000Z</LastModified><ETag>"00002e987b80919a095bf8da63d8399b"</ETag><Size>166892</Size><Owner><ID>07ba55931634353d81b28388db73b19c576cefcc3f5a19be3137e5cbf691f554</ID><DisplayName>admin</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents>

i want to read the text between all nodes named '<Key>' (without quotes).
can anyone have a suggestion. how to read ?? ❓ 🤐


* - Support™ - Mar 2, 2016

PHP Code

<?php 
Dim XML 
As XmlDocument = New XmlDocument()
XML.Load("Filepath.xml")
Dim KeysList As XmlNodeList = XML.GetElementsByTagName("Key")
For 
i=0 to KeysList.Count-1
Console
.WriteLine(KeysList.ItemOf(i).InnerXml)
Next 

I think this will do.


* - hunters - Mar 2, 2016

(Mar 2, 2016 11:11 AM)Support™ Wrote:  
PHP Code

<?php 
Dim XML 
As XmlDocument = New XmlDocument()
XML.Load("Filepath.xml")
Dim KeysList As XmlNodeList = XML.GetElementsByTagName("Key")
For 
i=0 to KeysList.Count-1
Console
.WriteLine(KeysList.ItemOf(i).InnerXml)
Next 

I think this will do.

thank you for your reply. i tried this but this code read only first node's text. not all nodes ('<Key>' node). 🤨


* - Strintent - Mar 2, 2016

Not sure if this is what you're looking for but I found a few links.



* - hunters - Mar 2, 2016

all of these links are lil bit helpful but cannot solve my issue, Sir. :(


* - Ninja - Mar 2, 2016

(Mar 2, 2016 12:56 PM)hunters Wrote:  all of these links are lil bit helpful but cannot solve my issue, Sir. :(

How about you post your code so we can help fix it? How do you expect we can fix your code if you havent posted your code?


* - Josh‎ - Mar 2, 2016

Pretty sure it's

Code
Dim XML As XmlDocument = New XmlDocument()
XML.Load("Filepath.xml")
Dim KeysList As XmlNodeList = XML.SelectNodes("Contents/Key")
For i=0 to KeysList.Count-1
Console.WriteLine(KeysList.ItemOf(i).InnerText)
Next


Or if the XML is in a string:

Code
Dim XMLString As String = "XML HERE"
Dim XML As XmlDocument = New XmlDocument()
XML.LoadString(XMLString)
Dim KeysList As XmlNodeList = XML.SelectNodes("Contents/Key")
For i=0 to KeysList.Count-1
Console.WriteLine(KeysList.ItemOf(i).InnerText)
Next



* - Hawk HF - Mar 2, 2016

(Mar 2, 2016 12:56 PM)hunters Wrote:  all of these links are lil bit helpful but cannot solve my issue, Sir. :(

Honestly this isn't that difficult.
Apply yourself instead of relying on others to provide a solution.


* - Support™ - Mar 2, 2016

(Mar 2, 2016 11:17 AM)hunters Wrote:  thank you for your reply. i tried this but this code read only first node's text. not all nodes ('<Key>' node). 🤨

It is because your XML is messed up.
You have multiple root elements with <Contents> tag and you will also need need to escape the double quotes(") in <ETag>.