Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/10/2006, 12:00
Emerald
 
Fecha de Ingreso: mayo-2006
Mensajes: 243
Antigüedad: 18 años
Puntos: 0
Pregunta No está guardando los cambios

Hola!

Encontré un ejemplo de como agregar nodos a un XML usando C#...
Pero no está guardando nada... en la consola se muestra el archivo como quedaría modificado, pero como que no está haciendo la instruccion:doc.Save(Console.Out);

Código:
using System;
using System.Xml;


namespace Microsoft.Framework.Samples
{
    class ModifyXmlDocumentSample
    {
        static void Main()
        {
            ModifyXmlDocumentSample xmlSample = new ModifyXmlDocumentSample();
            xmlSample.Run();
        }

        void InsertBook(XmlDocument doc)
        {

            XmlNode bookstore = doc.DocumentElement;

            XmlElement book = doc.CreateElement("book");
            book.SetAttribute("genre", "novel");
            book.SetAttribute("publicationdate", "1998");
            book.SetAttribute("ISBN", "0-553-21311-03");

            XmlElement title = doc.CreateElement("title");
            title.InnerText = "Moby Dick";
            book.AppendChild(title);

            XmlElement author = doc.CreateElement("author");
            XmlElement firstname = doc.CreateElement("first-name");
            firstname.InnerText = "Herman";
            XmlElement lastname = doc.CreateElement("last-name");
            lastname.InnerText = "Melville";
            author.AppendChild(firstname);
            author.AppendChild(lastname);
            book.AppendChild(author);

            XmlElement price = doc.CreateElement("price");
            price.InnerText = "12.99";
            book.AppendChild(price);

            bookstore.AppendChild(book);

        }

        public void Run()
        {
            //Load XML file
            XmlDocument doc = new XmlDocument();
            doc.Load("../../books.xml");
            Console.WriteLine("books.xml:");
            doc.Save(Console.Out);

            try
            {
                //add a new individual with book with title='Moby Dick'
                InsertBook(doc);
                Console.WriteLine("After Insert:");
                doc.Save(Console.Out);
            }
            finally
            {
                Console.Write("Press Enter to Exit");
                Console.ReadLine();
            }
        }
    }
}