我怎样才能在c#

时间:2016-07-20 05:47:53

标签: c# loops

更新问题:澄清一下,如果我的问题不明确,抱歉。

我们走了。我在这里尝试做的是根据其属性更改spefic标签的标签名称,并删除属性

在这个例子中,我需要将带有属性斜体的Emphasis name更改为i tag name, 和para text =“no”到p tag

这是我如何填充我的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());
}

程序的输出

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

这是正确的输出

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

我询问第一个循环的pass the output到下一个循环的原因是

用作INPUT到下一个循环是因为输出。

我希望我的问题现在很明确。

4 个答案:

答案 0 :(得分:2)

string input = "some text";
string[] pets = { "dog", "cat", "bird" };
input += " ";

foreach (string value in pets)
{
   input += " " + value;
}

Console.WriteLine(input);

对于精确的情况,或者更简单的单行可以是:

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

答案 1 :(得分:0)

如果我理解你的问题,在循环上方创建一个新变量,然后在循环结束时保存到它:

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

答案 2 :(得分:0)

您可以使用String.Join执行此操作而无需循环:

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

Here is working Example

以下代码可以帮助您纠正错误,我建议您不要使用+=代替StringBuilder这样使用:

    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

答案 3 :(得分:0)

您只需使用 String.Join 即可获得预期的输出。像..

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

希望这有帮助!