0

question updated: for clarification, sorry if my question is not clear.

here we go. what im trying to do here is change the tag name of a spefic tag based on its attribute, and delete the attribute

in this example i need to change Emphasis name with attribute italic to i tag name, and para textbreak="no" to p tag

here is how i populate my listview

 //add data to listview this.lvContent.Items.Add(new MyItem { Tag = "Emphasis", Attribute = "Type", Value = "Italic", NewTag = "i" }); this.lvContent.Items.Add(new MyItem { Tag = "Para", Attribute = "TextBreak", Value = "No", NewTag = "p" }); foreach (MyItem item in lvContent.Items) { XElement rootI = XElement.Parse(txtInput.Text); IEnumerable<XElement> Emphasis = from el in rootI.Descendants("" + item.Tag + "") where (string)el.Attribute("" + item.Attribute + "") == "" + item.Value + "" select el; foreach (XElement el in Emphasis) { el.Name = "" + item.NewTag + ""; } XElement xdoc = XElement.Parse(rootI.ToString()); var elementsToRemove = from elemet in xdoc.Descendants(item.NewTag) where elemet.Attribute(item.Attribute).Value == item.Value select elemet; foreach (var ee in elementsToRemove) { if (ee.Attribute(item.Attribute).Value == item.Value) { ee.RemoveAttributes(); } } Console.WriteLine(xdoc.ToString()); } 

the output of the program

<Abstract> <Heading>Abstract</Heading> <Para TextBreak="No">Some paragraph <i>q</i></Para> </Abstract> <Abstract> <Heading>Abstract</Heading> <p>Some paragraph <Emphasis Type="Italic">q</Emphasis></p> </Abstract> 

and this is the correct output

<Abstract> <Heading>Abstract</Heading> <p>Some paragraph <i>q</i></p> </Abstract> 

the reason im asking how to pass the output of the first loop to the next loop is to

be used as INPUT to the next loop is because of the output.

i hope my question is clear now.

5
  • What do you mean by data will be the output of the first loop ? What do exactly want to do in each iteration ? At least write the expected output. Commented Jul 20, 2016 at 5:50
  • What are you trying to achieve? Commented Jul 20, 2016 at 5:50
  • It's really not clear what your specific problem is. Read data from the textbox? Write to the textbox? Commented Jul 20, 2016 at 5:53
  • i already updated the question and the possible output. thank you. and im sorry im not good in english Commented Jul 20, 2016 at 5:57
  • I think you should use string.Replace or Regex replace.... Commented Jul 20, 2016 at 6:25

4 Answers 4

2
string input = "some text"; string[] pets = { "dog", "cat", "bird" }; input += " "; foreach (string value in pets) { input += " " + value; } Console.WriteLine(input); 

Or a simpler one-liner for precise case could be:

Console.WriteLine(input + " " + String.Join(" ", pets)); 
Sign up to request clarification or add additional context in comments.

3 Comments

Well, Congrats for appending my answer to this post, anyway the first option with lots of += will not be a good solution when compared to the second.
Just as an addition for this answer: I recommend using a StringBuilder for string manipulation.
i already updated the question with the problem i am facing. i hope the question is clear now
0

If I understood your question right, make a new variable above the loop, then save to it in the end of the loop like this:

string input = "some text"; string[] pets = { "dog", "cat", "bird" }; string manipulateddata; foreach (string value in pets) { manipulateddata = ;//whatever your loop should do (you can use manipulated data here to manipulate) } Console.Writeline(manipulateddata); 

1 Comment

i already updated the question with the problem i am facing. i hope the question is clear now
0

You can simply use String.Join to achieve your expected output. Like..

Console.WriteLine(input + String.Join(" ", pets)); 

Hope this helps!

Comments

0

You can use String.Join to do this without looping:

Console.WriteLine("{0} : {1}",input ,String.Join(" ",pets)); 

Here is working Example

The following code will help you to correct your mistake, I wont suggest you to follow += instead for that use StringBuilder like this:

 string input = "some text"; string[] pets = { "dog", "cat", "bird" }; StringBuilder outputBuilder= new StringBuilder(input); foreach(string pet in pets) { outputBuilder.Append(" "); outputBuilder.Append(pet); } Console.WriteLine("{0}",outputBuilder.ToString()); 

Example with StringBuilder

1 Comment

i already updated the question with the problem i am facing. i hope the question is clear now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.