How to assign xml content to a string explicitly

Do you know how I can explicitly assign xml content to a string? Example:

string myXml = " <?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> " 

I want to do this, but with a rather large file. I need this because I want to use it in my unit testing, but it shows a lot of errors when I try to insert content between quotation marks.

+4
source share
2 answers

You need a verbatim string literal (a string that starts with the @ character) with escaped quotes (i.e. use double "" instead of single " ):

  string myXml = @" <?xml version=""1.0""?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> "; 
+7
source

Using:

 string myXml = @" <?xml version=""1.0""?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> "; 

Or just save the XML in a file and load it into a variable at runtime using File.ReadAllText() :

 string myXml = File.ReadAllText("test.xml"); 
+3
source

Source: https://habr.com/ru/post/1491402/


All Articles