0

I want to form a string as <repeat><daily dayFrequency="10" /></repeat>

Wherein the value in "" comes from a textboxe.g in above string 10. I formed the string in C# as

@"<repeat><daily dayFrequency=""+ txt_daily.Text + "" /></repeat>" but i get the output as

<repeat><daily dayFrequency="+ txt_daily.Text+ " /></repeat>. How to form a string which includes the input from a textbox and also double quotes to be included in that string.

Ishan
  • 4,008
  • 32
  • 90
  • 153

4 Answers4

2

To insert the value of one string inside another you could consider string.Format:

string.Format("foo {0} bar", txt_daily.Text)

This is more readable than string concatenation.

However I would strongly advise against building the XML string yourself. With your code if the user enters text containing a < symbol it will result in invalid XML.

Create the XML using an XML library.

Related

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

Escape it with \ Back slash. putting @ in front wont do it for you

string str = "<repeat><daily dayFrequency=\"\"+ txt_daily.Text + \"\" /></repeat>";
Console.Write(str);

Output would be:

<repeat><daily dayFrequency=""+ txt_daily.Text + "" /></repeat>
Habib
  • 219,104
  • 29
  • 407
  • 436
0

You could do it like this:

var str = String.Format(@"<repeat><daily dayFrequency="{0}" /></repeat>",
                        txt_daily.Text);

But it would be best to have an object that mapped to this format, and serialize it to xml

nunespascal
  • 17,584
  • 2
  • 43
  • 46
0

string test = @"<repeat><daily dayFrequency=" + "\"" + txt_daily.Text + "\"" + "/></repeat>";

Ishan
  • 4,008
  • 32
  • 90
  • 153
andy
  • 5,979
  • 2
  • 27
  • 49