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
  • 3,908
  • 28
  • 83
  • 151

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
  • 767,688
  • 176
  • 1,542
  • 1,434
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
  • 212,447
  • 27
  • 392
  • 421
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,376
  • 2
  • 42
  • 45
0

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

Ishan
  • 3,908
  • 28
  • 83
  • 151
andy
  • 5,802
  • 2
  • 25
  • 49