XML Editing: How can I get the value of a property

2

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.CatchinguuidofMachineworks,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.

    
asked by anonymous 09.07.2015 / 02:20

1 answer

1
var valorUuid = "";
var doc = XDocument.Load(@"Z:\Área de Trabalho\Windows Vista.xml");
var machine = doc.Root.Element("Machine");
foreach (var element in doc.Root.Elements())
{
   if (element.Name.LocalName == "Machine") 
   {
       valorUuid = element.Attribute("uuid").Value;
       break;
   }
}
    
09.07.2015 / 19:02