Click here to Skip to main content
14,740,353 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi there please look at my below code how do i prevent the text from getting out the loop and starting the process all over again

private void btnXML_Click(object sender, EventArgs e)
        {
            XDocument doc = new XDocument();
            var root = new XElement("xmlcap"); 
            var main = new XElement("Main");
            var title = new XElement("title");
            var ingredients = new XElement("Ingredients");
            var method = new XElement("Method");
            var file = new XElement("file");
            string ingredientsText = string.Empty;

            title.Value = txtText.Lines[0];
            //new XElement("<p>" + line + "</p>"),

            foreach(var line in txtText.Lines.Skip(1))
            {
                if (line.Contains("Method"))
                    break;

                ingredientsText += string.Format("<p>{0}</p>\n", line);
            }

            ingredients.Value = ingredientsText;

            foreach(var line in txtText.Lines.Skip(1))
            {
                ingredientsText += string.Format("<p>{0}</p>\n", line);
            }

            method.Value = ingredientsText;

            main.Add(file);
            main.Add(method);
            main.Add(title);
            main.Add(ingredients);

            root.Add(main);

            doc.Add(root);

            txtXml.Text = doc.ToString();
        }<pre lang="c#">
Posted
Updated 27-Mar-14 6:54am
v2
Comments
Richard C Bishop 27-Mar-14 11:55am
   
You are doing the same loop twice. Just do it once and viola!
Nico_Travassos 27-Mar-14 11:58am
   
first loop is for ingredients and 2nd loop is for methods
Pheonyx 27-Mar-14 12:07pm
   
As you can see from the solutions, your question is a bit unclear in that no-one is exactly sure what you are trying to achieve. If none of the current solutions solve your issue, I suggest you use the "Improve Question" and adjust your question with clearer requirements.

I think I understand what you are trying to do, why not do something like this instead of your foreach loops:

bool isMethod = false;
var methodText = string.Empty;
foreach(var line in txtText.Lines.Skip(1))
{
   if (!isMethod &&  line.Contains("Method"))
   {
      isMethod = true;
      continue;
    }

    if(isMethod)
    {
        methodText += string.Format("&lt;p&gt;{0}&lt;/p&gt;\n", line);
    }
    else
    {
       ingredientsText += string.Format("&lt;p&gt;{0}&lt;/p&gt;\n", line);
    }
}

ingredients.Value = ingredientsText;
method.Value = methodText;
   
To get out of a loop use break[^].
   
I think you are trying to do this:
private void btnXML_Click(object sender, EventArgs e)
        {
            XDocument doc = new XDocument();
            var root = new XElement("xmlcap"); 
            var main = new XElement("Main");
            var title = new XElement("title");
            var ingredients = new XElement("Ingredients");
            var method = new XElement("Method");
            var file = new XElement("file");
            string ingredientsText = string.Empty;
 
            title.Value = txtText.Lines[0];
            //new XElement("<p>" + line + "</p>"),

            if (line.Contains("Method"))
            {
                 foreach(var line in txtText.Lines.Skip(1))
                 { 
                    ingredientsText += string.Format("<p>{0}</p>\n", line);
                 }
                 method.Value = ingredientsText;
            }
            else
            {
                 foreach(var line in txtText.Lines.Skip(1))
                 {
                     ingredientsText += string.Format("<p>{0}</p>\n", line);
                 }   
                 ingredients.Value = ingredientsText;
            }
 
            main.Add(file);
            main.Add(method);
            main.Add(title);
            main.Add(ingredients);
 
            root.Add(main);
 
            doc.Add(root);
 
            txtXml.Text = doc.ToString();
        }
   
Basically, you don't - The way I would do it is to just use the one loop:
foreach (var line in txtText.Lines.Skip(1))
    {
    if (line.Contains("Method"))
        {
        ingredients.Value = ingredientsText;
        ingredientsText = "";
        }
    ingredientsText += string.Format("&lt;p&gt;{0}&lt;/p&gt;\n", line);
    }
method.Value = ingredientsText;

But...you shouldn't use a string for that, it's very inefficient. Use a StringBuilder instead:
StringBuilder text = new StringBuilder();
foreach (var line in txtText.Lines.Skip(1))
    {
    if (line.Contains("Method"))
        {
        ingredients.Value = text.ToString();
        text.Clear();
        }
    text.AppendFormat("&lt;p&gt;{0}&lt;/p&gt;\n", line);
    }
method.Value = text.ToString(); ;


[edit] :doh: Left the "break"s in from the OP example :O - OriginalGriff[/edit]
   
v2
You have two loops. You just need to remove one of them.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900