• 0

[VB.Net] XML Documents


Question

Hello all,

I'm trying to parse XML from the Facebook API... First here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<photos_get_response xmlns="http://api.facebook.com/1.0/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
   <photo>
	 <pid>34585991612804</pid>
	 <aid>34585963571485</aid>
	 <owner>1240077</owner>
	 <src>http://ip002.facebook.com/v11/135/18/8055/s1240077_30043524_2020.jpg</src>
	 <src_big>http://ip002.facebook.com/v11/135/18/8055/n1240077_30043524_2020.jpg</src>
	 <src_small>http://ip002.facebook.com/v11/135/18/8055/t1240077_30043524_2020.jpg</src>
	 <link>http://www.facebook.com/photo.php?pid=30043524&id=8055</link>
	 <caption>From The Deathmatch (Trailer) (1999)</caption>
	 <created>1132553361</created>
   </photo>
</photos_get_response>

The code I have works fine if I remove the attributes from the 'photos_get_response' part, however with them there it won't work. I've tried reading the XPath guide on W3schools.com but to no avail. My source code is as follows:

		Dim xml As XmlDocument = New XmlDocument
		Dim nodelist As XmlNodeList
		Dim node As XmlNode
		Dim urllist As String = ""

		xml.LoadXml(TextBox1.Text)
		nodelist = xml.SelectNodes("/photos_get_response/photo")


		For Each node In nodelist
			urllist = urllist & node.ChildNodes.Item(4).InnerText & vbCrLf
		Next

		MsgBox(urllist)

Thank you to anyone out there who can help me!

Simsie

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

"<src_big>" and "<src_small>" ending tags dont match should be </src_small> and </src_big> not </src>

Also if you have time look into LINQ's (System.XML.LINQ) XDocument, it makes XmlDocument look like a mess.

Edited by Doli
Link to comment
Share on other sites

  • 0

Well spotted on the tags thing. I copied that XML frm the Facebook Developers Wiki, however the XML I'm trying to parse is live from Facebook and valid.

Also cheers for the LINQ idea, I'll look into that a bit later.

Link to comment
Share on other sites

  • 0

You need to make sure you register the namespace with the XmlDocument type:

Dim document As New System.Xml.XmlDocument()
document.Load((...)

Dim nsManager As New System.Xml.XmlNamespaceManager(document.NameTable)
nsManager.AddNamespace("fb", "http://api.facebook.com/1.0/")
nsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")

Dim nodeList As System.Xml.XmlNodeList
Dim node As System.Xml.XmlNode

nodeList = document.SelectNodes("//fb:photos_get_response/fb:photo", nsManager)
For Each node In nodeList
  'Do some work
Next

Looking into Linq-To-Xml is probably a good idea, its quick and easy :)

Link to comment
Share on other sites

  • 0
Well spotted on the tags thing. I copied that XML frm the Facebook Developers Wiki, however the XML I'm trying to parse is live from Facebook and valid.

Also cheers for the LINQ idea, I'll look into that a bit later.

You can use something like this:

Imports &lt;xmlns="http://api.facebook.com/1.0/"&gt;

urllist += String.Join(" ", (New XDocument(someUrl)).&lt;photos_get_response&gt;.&lt;photo&gt;.&lt;src_big&gt;.Select(Function(e) e.Value).ToArray)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.