public void Save() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Libro></Libro>");
XmlElement newElem = doc.CreateElement("name");
newElem.InnerText = "" + text;
doc.DocumentElement.AppendChild(newElem);
XmlElement newElem0 = doc.CreateElement("autor");
newElem0.InnerText = "" + Autor.autor;
doc.DocumentElement.AppendChild(newElem0);
XmlTextWriter writer = new XmlTextWriter(@"D:\data.xml", null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
}
}
<Libro>
<name>DATOS DE LA VARIABLE</name>
<autor>DATOS DE LA VARIABLE</autor>
</Libro>
Con el codigo de arriba consigo hacer ese xml,pero como podria hacer que el xml fuera asi?
<Libro>
<datos>
<name>DATOS DE LA VARIABLE</name>
<autor>DATOS DE LA VARIABLE</autor>
</datos>
</Libro>
Que tengo que modificar del codigo? alguna idea?
Hola,
me costo mucho hacer eso. Antes de leer tu pregunta no tenia ni una idea de como escribir archivos Xml. Pero cuando pueda dedico con gusto tiempo a personas como. Aqui tienes:
XmlDocument doc = new XmlDocument();
XmlElement root = doc.DocumentElement;
XmlElement element1 = doc.CreateElement(string.Empty, "Libro", string.Empty);
doc.AppendChild(element1);
XmlElement element2 = doc.CreateElement(string.Empty, "Datos", string.Empty);
element1.AppendChild(element2);
XmlElement element3 = doc.CreateElement(string.Empty, "Name", string.Empty);
XmlText text1 = doc.CreateTextNode("DATOS DE LA VARIABLE");
element3.AppendChild(text1);
element2.AppendChild(element3);
XmlElement element4 = doc.CreateElement(string.Empty, "Autor", string.Empty);
XmlText text2 = doc.CreateTextNode("DATOS DE LA VARIABLE");
element4.AppendChild(text2);
element2.AppendChild(element4);
doc.Save("C:\\Users\\X61s\\Desktop\\document.xml");
Saludos!