Generate an xml in C # [closed]

4

I need to create an application where the data will be saved to a local xml and then sent to a server using a wcf. But I have the following doubt what I do in the client and what I do in the service (wcf). I'm new to this area and I have no idea where I start. Could someone give me an example of how to do it?

    
asked by anonymous 03.05.2016 / 15:38

3 answers

6

You can do with LINQ to XML, it's very simple. I made a very basic example to be easy to understand:

public class Pessoa
{
    public string Nome { get; set; }
    public int Idade { get; set; }

    public Pessoa(string nome, int idade)
    {
        Nome = nome;
        Idade = idade;
    }
}

public static void Escrever()
{
    var pessoas = new[] { new Pessoa("Fulano", 10), new Pessoa("Ciclano", 20) };

    var xml = new XDocument(new XElement("Pessoas",
                                        pessoas.Select(p => new XElement("Pessoa",
                                            new XAttribute("Nome", p.Nome),
                                            new XAttribute("Idade", p.Idade)))));
    xml.Save("pessoas.xml");
}

public static void Ler()
{
    var xml = XDocument.Load("pessoas.xml");

    Pessoa[] pessoas = xml.Element("Pessoas").Elements("Pessoa")
                       .Select(x => new Pessoa(x.Attribute("Nome").Value,
                                               int.Parse(x.Attribute("Idade").Value))).ToArray();
}

Result:

<?xml version="1.0" encoding="utf-8"?>
<Pessoas>
  <Pessoa Nome="Fulano" Idade="10" />
  <Pessoa Nome="Ciclano" Idade="20" />
</Pessoas>

Following is a link to the Macoratti website: link

    
04.05.2016 / 01:14
4

Using the vitor model, you can also use the Serializable attribute class:

[Serializable]
public class Pessoa
{
    public string Nome { get; set; }
    public int Idade { get; set; }
    public Pessoa()
    {

    }
    public Pessoa(string nome, int idade)
    {
        Nome = nome;
        Idade = idade;
    }


    public static void Escrever()
    {
        Pessoa[] pessoas = new[] { new Pessoa("Fulano", 10), new Pessoa("Ciclano", 20) };

        System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer(typeof(Pessoa[]));

        using (System.IO.TextWriter Write = new System.IO.StreamWriter("pessoas.xml", true))
        {
            Serializer.Serialize(Write, pessoas);
            Write.Close();
        }
    }

    public static Pessoa Ler()
    {
        System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer(typeof(Pessoa));
        Pessoa pessoas = null;
        using (System.IO.TextReader Reader = new System.IO.StreamReader("pessoas.xml"))
        {
            pessoas = Serializer.Deserialize(Reader) as Pessoa;
            Reader.Close();
        }

        return pessoas;
    }
}
    
04.05.2016 / 02:06
4

What you want is a database environment, on-site , to work with your app offline, and when you have a connection, sync these data. Right?

Well, as it was not said if it is a desktop or mobile app, or assume that you are using .NET Framework , ie a desktop app.

For these cases, I exclusively recommend using System.Data.DataSet . DataSet is a representation, memory me, of a database. With it you can simulate an entire database, creating tables, making relationships, even controlling transactions.

But the feature most important in your case is that it already has methods to write and retrieve information in XML. See:

var dataSet = new DataSet();
dataSet.Tables.Add(new DataTable("Tabela1"));
dataSet.WriteXml(@"c:\bancodedados.xml");

Your scenario fits well with this guy. It's super easy to deploy, and it also has features for saving and retrieving local data in XML.

But be aware that it is not recommended - for me at least - to use this guy in any scenario. In yours, specifically, it fits.

A few months ago I wrote an article discouraging you to use it for its complexity. Talking about the DataSet and DataTable maladies . It's worth a look.

    
04.05.2016 / 13:44