Building XML file using xml Serializer - More efficient way

1

I am building an XML file whose structure must be nested tags as shown below:

Theaboveimageistheoutputtothefollowingcodebelow:

publicclassFeeder{publicstringName{get;set;}publicdoubleVMin{get;set;}publicdoubleVNom{get;set;}publicdoubleVMax{get;set;}publicdoubleFPMin{get;set;}}publicclassFeedersLimit{publicFeederfeederBase;//ClasseFeederéumatributo}publicclassFinancialLimite{publicintHorizonPlan{get;set;}publicintWacc{get;set;}}publicclassEngineerRules{publicintVarTensMax{get;set;}}//ClasseFeederLimite,FinancialLimteeEnginnerRulessãoatributosdessaClassepublicclassConfigurationStudy{publicFeedersLimitfeederLimit;publicFinancialLimitefinancialLimite;publicEngineerRulesenginnerRules;}

AndthecoderesponsibleforgeneratingmyXMLiswhatisbelow:

staticvoidMain(string[]args){ConfigurationStudyconfig=newConfigurationStudy();config.feederLimit=newFeedersLimit();config.financialLimite=newFinancialLimite();config.enginnerRules=newEngineerRules();config.feederLimit.feederBase=newFeeder();//SetandovaloresparaFeederBaseconfig.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();
    }

While this way of building my XML works, I believe it is not 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 question is:

  • Is there an easier way to build my XML following the proposed structure? Remembering that I can not change my structure

  • Any code structuring tips?

  • asked by anonymous 17.07.2017 / 14:02

    1 answer

    0

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

    They would look like this:

    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 do not have access to the class code, or if they are part of an external library or any other reason, you can do with LINQ as well. See my answer .

        
    22.07.2017 / 18:27