locked
How to create loop? RRS feed

  • Question

  • User647458646 posted

    The string a and string b return 3 set of values (count of 3). 

    I would like to create a condition, if the values from a and b are more than 1, then loop through XML document in order to output three set of xml document for those three values from string a and b.

    I am little unclear of the syntax I would use to construct the above condition, any reference or guide would be very much appreciated. 

    For example – desired output:

    String a = “title1, title2, title3”

    String b = “content1, content2, content3”

    <article> … </article>

    <article> … </article>

    <article> … </article> 

     public  string articleXMLTest()
            {
    
                string a = testtitle();
                string b = testStory();
                string c = "";
    
                DateTime dt = DateTime.Today;
    
                XElement xeRoot = new XElement("article");
     
                XElement xeStatus = new XElement("status", "Approved");
                xeRoot.Add(xeStatus);
    
                XElement xeTitle = new XElement("title", a);
                xeRoot.Add(xeTitle);
    
                XElement xeSubTitle = new XElement("subtitle", a);
                xeRoot.Add(xeSubTitle);
    
                XElement xeSynopsis = new XElement("synopsis", b + "...");
                xeRoot.Add(xeSynopsis);
    
                XElement xeURL = new XElement("url", c);
                xeRoot.Add(xeURL);
    
                XElement xeDisplayDate = new XElement("display_date", dt);
                xeRoot.Add(xeDisplayDate);
    
                XDocument xDoc = new XDocument(xeRoot);
                return xDoc.ToString();
            }

    Thank you

    Wednesday, June 22, 2016 2:10 PM

Answers

  • User36583972 posted

    Hi missy786,

    if the values from a and b are more than 1, then loop through XML document in order to output three set of xml document for those three values from string a and b.

    I have made a sample, you can refer the following code.

                string a = "title1, title2, title3";
                string b = "content1, content2, content3";
    
                string[] aA = a.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] bA = b.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if(aA.Length ==bA .Length)
                {
                    for (int i =0; i<aA.Length;i++ )
                    {
                        DateTime dt = DateTime.Today;
    
                        XDocument doc = new XDocument(
                   new XDeclaration("1.0", "gb2312", string.Empty),
                   new XElement("article",
                       new XElement("status", "Approved"),
                        new XElement("title", aA[i].ToString()),
                         new XElement("subtitle", aA[i].ToString()),
                          new XElement("synopsis", bA[i].ToString() + "..."),
                           new XElement("url", "url:llll"),
                            new XElement("display_date", dt.ToShortDateString())
                     ));
                        TextBox3.Text = TextBox3.Text + Environment.NewLine + doc.ToString();
                        
                    }
                }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 23, 2016 7:24 AM