askquestionzero
askquestionzero

Reputation: 107

How can i do this in c#

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.

Upvotes: 0

Views: 90

Answers (4)

sujith karivelil
sujith karivelil

Reputation: 29036

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

Upvotes: 0

Abhishek
Abhishek

Reputation: 170

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

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

Hope this helps!

Upvotes: 0

RakouskyS
RakouskyS

Reputation: 54

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);

Upvotes: 0

VidasV
VidasV

Reputation: 4895

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));

Upvotes: 2

Related Questions