1
I am mounting an XML file whose structure should be have nested tags as the image below:
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:
There is an easier way to build my XML by following the proposed structure? Remembering that I can’t change my structure
Some hint of code structuring?