C# XML file handling - Library tips

Asked

Viewed 318 times

1

I need to do the manipulation of an XML file in C#. This is the first time I will do this, and I would like some tips. My algorithm basically is this:

  1. Fill an XML from my system data (DB Data).

    Ex: I have an entity from which comes my data example Person, that the same by your turn has its attributes.

    I need to take this data from Pessoa, and assemble an xml.

  2. Read this generated XML file, for execution of an X algorithm, which will perform calculations based on my XML data.

    This algorithm will provide me with new data as a result of my study.

  3. After the execution of algorithm X, generate an XML with the obtained results, from algorithm X.

I need tips on:

In short what I desire is:

  • Generate an xml with data from my entity,
  • read this file populating a result entity.
  • Export my output to xml.

My point is:

1 - Which Lib can I use to manipulate these files? That is, there is a library you can use to help me manipulate xml files in C#. What I mean is, not to have to do everything by hand?

2 - Someone has an example that can provide me, or some hint on how I can accomplish this task?

3 - Lib to automate this task?

Grateful.

  • your question is very wide, because it does not put a scenario that there on this can solve most of your doubts!

  • I think you can start searching for Entity Framework, to model your BD on C#objects. From there, search by Linq to XML (allows you to create, change, xml files).

  • @Renatoafonso Thanks for the tip

1 answer

2


C# provides some classes (Xmlserializer, Streamwriter, Streamreader, etc...) of namespaces System.Xml and System.IO that you can use to manipulate files and make XML serialization.

If you can’t find a Lib to automate, follow an example below:

using System.IO;
using System.Xml.Serialization;

class Program
{
    static void Main(string[] args)
    {
        // Gerar um xml com os dados de minha entidade

        var pessoa = new Pessoa { Id = 1, Nome = "Renan" };

        var xmlSerializer = new XmlSerializer(typeof(Pessoa));
        StreamWriter streamWriter = new StreamWriter("pessoa.xml");

        xmlSerializer.Serialize(streamWriter, pessoa);

        streamWriter.Close();

        // ler esse arquivopopulando uma entidade de resultados.
        // Exportar meu resultado para xml.

        FileStream meuFileStream = new FileStream("pessoa.xml", FileMode.Open);

        Pessoa _pessoa = (Pessoa)xmlSerializer.Deserialize(meuFileStream);

        Console.WriteLine(_pessoa.Nome); // Imprime "Renan"
        Console.ReadLine();
    }
}

public class Pessoa
{
    public int Id { get; set; }

    public string Nome { get; set; }
}

Xml generated and saved in "... bin Debug person.xml":

<?xml version="1.0" encoding="utf-8"?>
<Pessoa xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>1</Id>
  <Nome>Renan</Nome>
</Pessoa>
  • Grateful for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.