Visual Basic has XML literals so you can include them directly in your code as in the following example.
Dim employees As XElement =
<employees>
<employee firstname="Terry" lastname="Pratchett"/>
<employee firstname="Glen" lastname="Cook"/>
<employee firstname="Tom" lastname="Holt"/>
<employee>
<firstname>Rod</firstname>
<lastname>Stephens</lastname>
</employee>
</employees>
C# doesn't have XML literals but you can fake them fairly easily. Invoke the XElement class's Parse method passing it the XML text as a string beginning with the @ character. Strings that begin with @ can span multiple lines so you can make this look a lot like an XML literal.
The only restriction is that the string cannot contain any double quotes because then C# would think the string had ended. Usually in XML text you can use single quotes instead of double quote. If you really need to use double quotes, you can double them up as in "".
When the program starts, it executes the following code. The XML "literal" is shown in blue.
The code creates an XElement variable and sets it equal to the result returned by the XElement.Parse method. It passes the method some XML text. Notice that it uses double double quotes to delimit the values for Terry Pratchett.
After it loads the XML text, the program loops through the XElement's nodes and displays their text values in the program's ListBox.
Comments