创建动态XML类

时间:2016-10-18 10:36:53

标签: c# xml

我有一个将序列化的“MsrProgram”类。但是,如果“MsrProgram”中的参数“Number”不同,我的XML文件中需要不同的参数。这样做的最简单方法是什么?

这是我的代码:

public class MsrProgram
{
    [XmlAttribute]
    public string OwnerTypeFullName { get; set; }

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

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

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

public class main
{
   var toolList = (from pos in Configuration.List
      select new Position
      {
         ToolNumber = (int)pos.tlno,
         Tool =
         {
            ToolId = pos.tlno.ToString(),
            Step =
            {
               Position = "1",
               MsrProgram =
               {
                  OwnerTypeFullName = "",
                  Number = 1,
                  MsrRange = "1", //When Number is 1
                  TurnoverMeasure = "1", //When Number is 2
               }
            }
         }
      }
}

1 个答案:

答案 0 :(得分:2)

您的代码并未显示所有内容,因此我无法提供完整的代码,但这应该可以帮助您:

var toolList = (from pos in Configuration.List
    select new Position
    {
        ToolNumber = (int)pos.tlno,
        Tool = new Tool
        {
            ToolId = pos.tlno.ToString(),
            Step = new Step
            {
                Position = "1",
                MsrProgram = new MsrProgram
                {
                    OwnerTypeFullName = "",
                    Number = GetNumber(), // <- fill in what really should be used
                    MsrRange = GetNumber() == 1 ? 1 : 0,
                    TurnoverMeasure = GetNumber() == 2 ? 1 : 0
                }
            }
        }
    }
);

我还添加了一些您错过或遗忘的new ...个陈述。