首页 > 解决方案 > 将 .cs 变量转换为 .xml 文件

问题描述

我在 C# 中有这个程序。我想将其转换为 XML 文件,并且我想在 //Input 和 //Output 之间进行区分。例如,它应该在读取 //Input 时开始写入,并在读取 //Output 时开始写入其他内容。我知道我可以手动编写所有内容以将其转换为 XML 文件,但我需要它自动完成,这样当我稍后添加一些变量时,我不必手动将它们写入 XML 文件。

class Program
{
    //Input
    public static bool Power = true;     
    public static bool Airflow = true;       
    //Output
    public static bool Cylinder1_Working;

    public static bool Cylinder2_Working;

    static void Main(string[] args)
    {
        if ((Power == true) && (Airflow == true))
        {
            Cylinder1_Working = true;
            Cylinder2_Working = true;
        }         
    }
}

我需要把上面的转换成下面的:

<LogicDefinition xsi:type="CSharpLogicDefinition"  LogicName="Program">
  <InputVariables>
    <Variable>
      <Name>Power</Name>
      <Type>Bool</Type>
      <Mode>Input</Mode>
    </Variable>
    <Variable>
      <Name>Airflow</Name>
      <Type>Bool</Type>
      <Mode>Input</Mode>
    </Variable>
  </InputVariables>  

  <OutputVariables>
    <Variable>
      <Name>Cylinder1_Working</Name>
      <Type>Bool</Type>
      <Mode>Output</Mode>
    </Variable> 
    <Variable>
      <Name>Cylinder2_Working</Name>
      <Type>Bool</Type>
      <Mode>Output</Mode>
    </Variable>
  </OutputVariables>

  <Version Major="1" Minor="1" Patch="0"  />

  <UserText>
     if ((Power == true) &amp;&amp; (Airflow == true)) {
       Cylinder1_Working = true;
       Cylinder2_Working = true;
     }
  </UserText>
</LogicDefinition>

我已经查看了[Serialize]可以将其转换为 XML 的位置,但我不知道如何自行转换所有内容。我只需要一种方法来解决我的问题,因为谷歌无法帮助我解决这个问题。

标签: c#xmlvariables

解决方案


推荐阅读