Home > Blockchain >  How to create an loop that will implement my XML lines the amount of times i want it
How to create an loop that will implement my XML lines the amount of times i want it

Time:02-10

Is there any way where I can make an loop of an whatever size and it will loop over the number given and add the XML elements. The first part of the code should remain like this and the down part should be dynamic.

string name = "haus";
XDocument xmlDocument = new XDocument(
  new XDeclaration("1.0", "utf-8", "yes"),
  new XElement("Document",
    new XElement("DocumentInfo",
      new XElement("Created", "2022-02-09T13:52:07.8908726Z"),
      new XElement("ExportSetting", "WithDefaults"),
      new XElement("InstalledProducts",
        new XElement("Product",
          new XElement("DisplayName", "Totally Integrated Automation Portal"),
          new XElement("DisplayVersion", "V16")
        ),
        new XElement("OptionPackage",
          new XElement("DisplayName", "TIA Portal Version Control Interface"),
          new XElement("ObjectList",
            new XElement("MultilingualText", 
              new XAttribute("ID", 1), 
              new XAttribute("CompositionName", "Comment"),
              new XElement("ObjectList",
                new XElement("MultilingualTextItem", 
                  new XAttribute("ID", 2), 
                  new XAttribute("CompositionName", "Items"),
                  new XElement("AttributeList",
                    new XElement("Culture", "de-DE"),
                    new XElement("Text")
                  )
                )
              )
            ),
            // From here it should do this for the given number for example 10
            new XElement("Hmi.TextGraphicList.TextListEntry", 
              new XAttribute("ID", 3), 
              new XAttribute("CompositionName", "Entries"),
              new XElement("AttributeList",
                new XElement("DefaultEntry", "false"),
                new XElement("EntryType","SingleValue"),
                new XElement("From","0"),  // this number should go up by one after every example
                new XElement ("To","0") // same with this
              ),  
              new XElement("ObjectList",
                new XElement("MultilingualText", 
                  new XAttribute("ID", 4), 
                  new XAttribute("CompositionName", "Text"),
                  new XElement("ObjectList",
                    new XElement("MultilingualTextItem", 
                      new XAttribute("ID", 5), 
                      new XAttribute("CompositionName", "Items"),
                      new XElement("AttributeList",
                        new XElement("Culture","de-DE"),
                        new XElement("Text",
                          new XElement("body",
                            new XElement("p","kein 0")
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  )
);

xmlDocument.Save(@"C:\Program Files (x86)\Tools\VisualStudioProjects\CustomersDetails.xml");

CodePudding user response:

Use a combination of Enumerable.Range and Select to generate the repeated XML elements:

XDocument xmlDocument = new XDocument(
  new XDeclaration("1.0", "utf-8", "yes"),
  new XElement("Document",
    new XElement("DocumentInfo",
      new XElement("Created", "2022-02-09T13:52:07.8908726Z"),
      new XElement("ExportSetting", "WithDefaults"),
      new XElement("InstalledProducts",
        new XElement("Product",
          new XElement("DisplayName", "Totally Integrated Automation Portal"),
          new XElement("DisplayVersion", "V16")
        ),
        new XElement("OptionPackage",
          new XElement("DisplayName", "TIA Portal Version Control Interface"),
          new XElement("ObjectList",
            new XElement("MultilingualText", 
              new XAttribute("ID", 1), 
              new XAttribute("CompositionName", "Comment"),
              new XElement("ObjectList",
                new XElement("MultilingualTextItem", 
                  new XAttribute("ID", 2), 
                  new XAttribute("CompositionName", "Items"),
                  new XElement("AttributeList",
                    new XElement("Culture", "de-DE"),
                    new XElement("Text")
                  )
                )
              )
            ),
            Enumerable.Range(0, 10).Select(i => new XElement("Hmi.TextGraphicList.TextListEntry", 
              new XAttribute("ID", 3), 
              new XAttribute("CompositionName", "Entries"),
              new XElement("AttributeList",
                new XElement("DefaultEntry", "false"),
                new XElement("EntryType","SingleValue"),
                new XElement("From", i),
                new XElement ("To", i)
              ),  
              new XElement("ObjectList",
                new XElement("MultilingualText", 
                  new XAttribute("ID", 4), 
                  new XAttribute("CompositionName", "Text"),
                  new XElement("ObjectList",
                    new XElement("MultilingualTextItem", 
                      new XAttribute("ID", 5), 
                      new XAttribute("CompositionName", "Items"),
                      new XElement("AttributeList",
                        new XElement("Culture","de-DE"),
                        new XElement("Text",
                          new XElement("body",
                            new XElement("p","kein 0")
                          )
                        )
                      )
                    )
                  )
                )
              )
            ))
          )
        )
      )
    )
  )
);
  • Related