Training
Module
Store and retrieve data using literal and variable values in C# - Training
Use data in your applications by creating literal values and variable values of different data types.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
You can create an XML document, fragment, or element directly in code by using an XML literal. The examples in this topic demonstrate how to create an XML element that has three child elements, and how to create an XML document.
You can also use the LINQ to XML APIs to create LINQ to XML objects. For more information, see XElement.
Create the XML inline by using the XML literal syntax, which is the same as the actual XML syntax.
Dim contact1 As XElement =
<contact>
<name>Patrick Hines</name>
<phone type="home">206-555-0144</phone>
<phone type="work">425-555-0145</phone>
</contact>
Run the code. The output of this code is:
<contact>
<name>Patrick Hines</name>
<phone type="home">206-555-0144</phone>
<phone type="work">425-555-0145</phone>
</contact>
Create the XML document inline. The following code creates an XML document that has literal syntax, an XML declaration, a processing instruction, a comment, and an element that contains another element.
Dim libraryRequest As XDocument =
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="show_book.xsl"?>
<!-- Tests that the application works. -->
<books>
<book/>
</books>
Console.WriteLine(libraryRequest)
Run the code. The output of this code is:
<?xml-stylesheet type="text/xsl" href="show_book.xsl"?>
<!-- Tests that the application works. -->
<books>
<book/>
</books>
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Store and retrieve data using literal and variable values in C# - Training
Use data in your applications by creating literal values and variable values of different data types.
Events
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in