I have a question to read and edit an XML in C Sharp. I need to get the value of the "uuid" attribute of the "Machine" node, which is in the "Virtualbox" node. I also want the "location" and "uuid" properties of each of the "HardDisk" nodes, as in the image. Here is the XML file link
ImadethiscodewiththehelpoftheuserpsNytrancez.Catchinguuid
ofMachine
works,buttogotoalowerlevelon'HardDisk',Ididnotgetresults.
vardoc=XDocument.Load(@"Z:\Área de Trabalho\Windows XP.xml");
var no = doc.Root.Element("Machine");
foreach (var elemento in doc.Root.Elements())
{
if (elemento.Name.LocalName == "Machine")
{
Console.WriteLine("Anterior: -" + elemento.Attribute("uuid").Value + "-");
break;
// Essa parte funciona perfeitamente.
}
}
foreach (var elemento in doc.Root.Elements())
{
if (elemento.Name.LocalName == "HardDisk")
{
Console.Write("-" + elemento.Attribute("uuid").Value + "-");
Console.WriteLine("-" + elemento.Attribute("location").Value + "-");
//Mas esta não lista nenhum resultado, sendo que tem dois no arquivo XML
}
}
The most interesting thing for me is a for or foreach to go through each of the sub nodes of HardDisks
, so no matter the number of nodes.
Thanks guys.