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?