Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign xml content to a string explicitly

Do you know how I can explicitlyt 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 quite bigger file.I need it because I want to use it in my Unit testing but it shows lots of errors when I am trying to paste the content between the quotes.

like image 727
mathinvalidnik Avatar asked Jul 15 '13 09:07

mathinvalidnik


People also ask

Can we store XML as a string?

You can parse the XML file using XPath expression, you can do XSL transformation, XSD schema validation and you can even parse whole XML file as String in just a couple of lines of code.

How do I create an XML string?

Use the XmlDocument class to create an XML document. Remember though that when you use CLOB datatype, you'll have to pass the value as a string. Here's a quick example that creates a simple XML document: XmlDocument doc = new XmlDocument(); XmlNode nodeOne = doc.


2 Answers

You need to verbatim string literal (string which starts with @ symbol) 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>
";
like image 162
Sergey Berezovskiy Avatar answered Oct 07 '22 16:10

Sergey Berezovskiy


Use:

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 store the XML in a file and load it into the variable at runtime using File.ReadAllText():

string myXml = File.ReadAllText("test.xml");
like image 30
Alex Filipovici Avatar answered Oct 07 '22 15:10

Alex Filipovici