How to add an attribute to an existing XML node?

This code doesn’t work

XmlElement root = LotDocument.DocumentElement;
		XmlNode newItem = LotDocument.CreateElement("item");
		XmlAttribute newId = LotDocument.CreateAttribute("id");
		
		
		foreach(XmlElement node in LotDocument.FirstChild){
			if(node.Name == "item" && !node.HasAttributes){
				node.SetAttributeNode(newId);
			}
		}
		
		root.AppendChild(newItem);
		LotDocument.Save(path); 

When I open the xml file afterwards the item node has been added without the ID attribute.

Any help is appreciated

Here is a snippet from something I did, you may find it helpful:

			xdoc = new XmlDocument();

			XmlNode docNode = xdoc.CreateXmlDeclaration("1.0", "UTF-8", null);
			xdoc.AppendChild(docNode);

			XmlNode rootNode = xdoc.CreateElement("X3D");
			XmlAttribute rootAttrib = xdoc.CreateAttribute("profile");
			rootAttrib.Value = "Immersive";
			rootNode.Attributes.Append(rootAttrib);
			xdoc.AppendChild(rootNode);