I've modified your code a 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");
Because XML has a namespace defined, it is recommended that you use a XmlNamespaceManager
to correctly read the node:
arquivo.Load(@"D:\Downloads\Windows XP.xml");
var nsmgr = new XmlNamespaceManager(arquivo.NameTable);
nsmgr.AddNamespace("vbox", "http://www.innotek.de/VirtualBox-settings");
Therefore, all subsequent readings need to consider the namespace . My final code looks 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"]);
}