Beispiel #1
0
        public void Put(int id, [FromBody] Studente value)
        {
            // Carico il file XML
            XDocument Dati     = XDocument.Load($"{_env1.ContentRootPath}/{pathToDataFile}");
            var       studente = from s in Dati.Element("root").Elements("persona")
                                 where s.Attribute("id").Value == id.ToString()
                                 select s;

            if (studente.Count() > 0)
            {
                // Modifico un elemento "persona"...
                studente.First().Attribute("nome").Value    = value.Nome;
                studente.First().Attribute("cognome").Value = value.Cognome;

                // lo salvo...
                Dati.Save($"{_env1.ContentRootPath}/{pathToDataFile}");
            }
        }
Beispiel #2
0
        public void Post(int id, [FromBody] Studente value)
        {
            // Carico il file XML
            XDocument Dati     = XDocument.Load($"{_env1.ContentRootPath}/{pathToDataFile}");
            var       studente = from s in Dati.Element("root").Elements("persona")
                                 where s.Attribute("id").Value == id.ToString()
                                 select s;

            if (studente.Count() == 0)
            {
                // Aggiungo un elemento "persona"...
                Dati.Element("root").Add
                (
                    new XElement("persona",
                                 new XAttribute("id", id),
                                 new XAttribute("nome", value.Nome),
                                 new XAttribute("cognome", value.Cognome))
                );

                // lo salvo...
                Dati.Save($"{_env1.ContentRootPath}/{pathToDataFile}");
            }
        }