Build XML File Tip Using xml Serializer - More Efficient Way

Asked

Viewed 160 times

1

I am mounting an XML file whose structure should be have nested tags as the image below:

inserir a descrição da imagem aqui

The image above is the output for the code below:

public class Feeder
{
   public string Name { get; set; }
   public double VMin { get; set; }
   public double VNom { get; set; }
   public double VMax { get; set; }
   public double FPMin { get; set; }
}

public class FeedersLimit
{
   public Feeder feederBase; //Classe Feeder é um atributo
}

public class FinancialLimite
{
    public int HorizonPlan { get; set; }
    public int Wacc { get; set; }
}

public class EngineerRules
{
    public int VarTensMax { get; set; }
}

// Classe FeederLimite, FinancialLimte e EnginnerRules são atributos dessa Classe

public class ConfigurationStudy
{
    public FeedersLimit feederLimit;
    public FinancialLimite financialLimite;
    public EngineerRules enginnerRules;
}

And the code responsible for generating my XML is what is below:

static void Main(string[] args)
    {

        ConfigurationStudy config = new ConfigurationStudy();
        config.feederLimit = new FeedersLimit();
        config.financialLimite = new FinancialLimite();
        config.enginnerRules = new EngineerRules();
        config.feederLimit.feederBase = new Feeder();

        //Setando valores para FeederBase
        config.feederLimit.feederBase.Name = "CJB-F2";
        config.feederLimit.feederBase.VMin = 0.93;
        config.feederLimit.feederBase.VNom = 13.8;
        config.feederLimit.feederBase.VMax = 1.05;
        config.feederLimit.feederBase.FPMin = 0.92;

        //Setando Valores para FinancialLimite
        config.financialLimite.HorizonPlan = 5;
        config.financialLimite.Wacc = 2;

        //Setando valores para EnginnerRules
        config.enginnerRules.VarTensMax = 5;

        var xmlSerializer = new XmlSerializer(typeof(ConfigurationStudy));
        StreamWriter streamWriter = new StreamWriter("EstudoAlocacao.xml");

        xmlSerializer.Serialize(streamWriter, config);
        streamWriter.Close();

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

        ConfigurationStudy _config = (ConfigurationStudy)xmlSerializer.Deserialize(meuFileStream);

        Console.WriteLine(_config.feederLimit);
        Console.WriteLine(_config.feederLimit.feederBase);
        Console.WriteLine(_config.feederLimit.feederBase.Name);
        Console.WriteLine(_config.feederLimit.feederBase.VMin);
        Console.WriteLine(_config.feederLimit.feederBase.VNom);
        Console.WriteLine(_config.feederLimit.feederBase.VMax);
        Console.WriteLine(_config.feederLimit.feederBase.FPMin);

        Console.WriteLine(_config.financialLimite);
        Console.WriteLine(_config.financialLimite.HorizonPlan);
        Console.WriteLine(_config.financialLimite.Wacc);

        Console.WriteLine(_config.enginnerRules);
        Console.WriteLine(_config.enginnerRules.VarTensMax);

        Console.ReadLine();
    }

Although this way of building my XML work, I don’t think it’s the most efficient way to do it. Because I will have many nested tags, and the XML size I need to generate is relatively large.

My point is:

  1. There is an easier way to build my XML by following the proposed structure? Remembering that I can’t change my structure

  2. Some hint of code structuring?

1 answer

0

Since you are using the XmlSerializer and wants to decrease the size of the generated files, my suggestion is to mark, in its classes, the properties of primitive types (double, string, int, etc.) with the attribute [XmlAttribute].

They would stay that way:

public class Feeder
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlAttribute]
    public double VMin { get; set; }

    [XmlAttribute]
    public double VNom { get; set; }

    [XmlAttribute]
    public double VMax { get; set; }

    [XmlAttribute]
    public double FPMin { get; set; }
}

public class FeedersLimit
{
    public Feeder feederBase;
}

public class FinancialLimite
{
    [XmlAttribute]
    public int HorizonPlan { get; set; }

    [XmlAttribute]
    public int Wacc { get; set; }
}

public class EngineerRules
{
    [XmlAttribute]
    public int VarTensMax { get; set; }
}

public class ConfigurationStudy
{
    public FeedersLimit feederLimit;
    public FinancialLimite financialLimite;
    public EngineerRules enginnerRules;
}

So when these properties are serialized, they form attributes in XML instead of elements. The file size is reduced and its content is even more readable, in my opinion:

<?xml version="1.0" encoding="utf-8"?>
<ConfigurationStudy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <feederLimit>
    <feederBase Name="CJB-F2" VMin="0.93" VNom="13.8" VMax="1.05" FPMin="0.92" />
  </feederLimit>
  <financialLimite HorizonPlan="5" Wacc="2" />
  <enginnerRules VarTensMax="5" />
</ConfigurationStudy>

Now, if you don’t have access to class code, or they are part of an external library or any other reason, you can do it with LINQ as well. Look at my answer.

Browser other questions tagged

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