none
XDocument execute chunk of code within XElement RRS feed

  • Question

  • I have a script to write a collada dae from a file and I need it to repeat. My program will be able to calculate the length of the file so it can repeat the necessary amount of times to complete. The code below is what I would be aiming for if it had worked.
    int i = 0;
    int amount = "amount of loops required";
    new XElement("name", while(i <= amount) {
    
    new XElement("name","data");
    i++;
    });

     

    Griffin

    Friday, October 18, 2019 12:44 AM

Answers

  • Check this example:

    int amount = 10;
    var element = new XElement( "name" );
    var element2 = new XElement( "name2" );
    element.Add( element2 );
    for( int i = 0; i < amount; ++i )
    {
       var child = new XElement( "name", "data" );
       element2.Add( child );
    }
    

    • Marked as answer by Garlfin Friday, October 18, 2019 8:41 PM
    Friday, October 18, 2019 11:55 AM

All replies

  • Try this:

    int amount = 10;

    var element = new XElement( "name" );

    for( int i = 0; i < amount; ++i )

    {

           var child = new XElement( "name", "data" );

           element.Add( child );

    }

     

    // checking the XML result:

    string xml = element.ToString( SaveOptions.None );

     

    /*

           Result:

           <name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

                  <name>data</name>

           </name>

           */

     

    If you need a different result, then give details.

    Friday, October 18, 2019 5:44 AM
  • Hi Garlfin, 

    Thank you for posting here.

    According to your question, I make a test on my side.

    You can refer to the following code to create a xml file.

                string path = @"path";
                XDocument xdoc = new XDocument();                    
                XElement root = new XElement("name");
                List<string> lst = new List<string>() {"A","B","C","D","E" };
                for (int i = 0; i < lst.Count; i++)
                {
                    XElement child = new XElement("name", lst[i]);
                    child.SetAttributeValue("id",i);
                    child.SetElementValue("age",i*5);
                    root.Add(child);
                }
                xdoc.Add(root);
                xdoc.Save(path);

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 18, 2019 7:19 AM
    Moderator
  • I just noticed I wasn't very specific. 
    int i = 0;
    int amount = "amount of loops required";
    new XElement("name",new XElement("name2", while(i <= amount) {
    
    new XElement("name","data");
    i++;
    }));
    
    
    This is more like what I am looking for

    Griffin

    Friday, October 18, 2019 11:50 AM
  • Check this example:

    int amount = 10;
    var element = new XElement( "name" );
    var element2 = new XElement( "name2" );
    element.Add( element2 );
    for( int i = 0; i < amount; ++i )
    {
       var child = new XElement( "name", "data" );
       element2.Add( child );
    }
    

    • Marked as answer by Garlfin Friday, October 18, 2019 8:41 PM
    Friday, October 18, 2019 11:55 AM
  • It looks like it should do it! I’ll try it when I get home.

    Griffin

    Friday, October 18, 2019 12:41 PM
  • It works! Thank you so much.

    Griffin

    Friday, October 18, 2019 8:41 PM