2
I have a question to read and edit an XML in C Sharp. I need to get the value of the attribute "uuid" of the "Machine" node, this one 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
I made this code with the help of the user psNytrancez. Catch the uuid
of Machine
works, but to go to a lower level, in `Harddisk , I could not result.
var doc = 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 for me is a for or foreach to go through each of the sub nodes of HardDisks
, so no matter the number of us.
Thanks people.
Can you please replace the image XML with text? I would like to run some tests here.
– Leonel Sanches da Silva
I put an answer but in order to ensure that it will work please put the full xml.
– Arthur Menezes
Thanks anyway. Your code worked right, but now I have a different problem. I supplemented my question, Now I need to get the value of a node at a lower level and my adaptation of what you gave me doesn’t work in this case.Open the XML link, the file is now another but has the same structure
– Marcelo Nascimento