I modified your code a little bit for the following:
XmlDocument arquivo = new XmlDocument();
arquivo.Load(@"D:\Downloads\Windows XP.xml");
// Esta linha abaixo gera a exceção
// "Referência de objeto não definida para uma instância de um objeto."
var lista = arquivo.SelectSingleNode("VirtualBox");
var lista2 = lista.SelectSingleNode("Machine")
.SelectSingleNode("MediaRegistry")
.SelectSingleNode("HardDisks")
.ChildNodes;
foreach (XmlNode item in lista)
{
Console.WriteLine(item.Attributes["uuid"]);
}
lista
is already null. It means that the selection already fails in it:
var lista = arquivo.SelectSingleNode("VirtualBox");
How XML has a namespace defined, the recommended is to use a XmlNamespaceManager
to read the node correctly:
arquivo.Load(@"D:\Downloads\Windows XP.xml");
var nsmgr = new XmlNamespaceManager(arquivo.NameTable);
nsmgr.AddNamespace("vbox", "http://www.innotek.de/VirtualBox-settings");
Therefore, all readings need to consider the namespace. My final code was like this, working:
XmlDocument arquivo = new XmlDocument();
arquivo.Load(@"D:\Downloads\Windows XP.xml");
var nsmgr = new XmlNamespaceManager(arquivo.NameTable);
nsmgr.AddNamespace("vbox", "http://www.innotek.de/VirtualBox-settings");
var lista = arquivo.SelectSingleNode("//vbox:VirtualBox", nsmgr);
var lista2 = arquivo.SelectSingleNode("//vbox:Machine", nsmgr);
var lista3 = lista2.SelectSingleNode("//vbox:MediaRegistry", nsmgr).SelectSingleNode("//vbox:HardDisks", nsmgr).ChildNodes;
foreach (XmlNode item in lista2)
{
Console.WriteLine(item.Attributes["uuid"]);
}