0

I have a class "MsrProgram" that will serialize. However, if the parameter "Number" in "MsrProgram" is different, i need different parameters in my XML File. What is the easyest way to do somthing like this?

Here is my code:

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
               }
            }
         }
      }
}
3
  • Do you mean an if statement? Commented Oct 18, 2016 at 10:39
  • Yes i need an if statement. But where can i do this? Commented Oct 18, 2016 at 10:46
  • Use a tertiary if statement or use a function. You could also refactor your linq into a foreach loop which may make it easier. Your question is really not very clear though Commented Oct 18, 2016 at 11:00

1 Answer 1

2

Your code does not show everything so I can not give complete code, but this should get you going:

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
                }
            }
        }
    }
);

I also added several new ... statements which you missed or forgot.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.