LINQ to XML (XElement) - How to create XML element
CreateXElement.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Xml.Linq" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
XElement singleXElement = new XElement("book", "Professional ASP.NET Design Patterns");
XElement multipleXElement = new XElement("books",
new XElement("book",
new XElement("name", "Professional ASP.NET MVC 3"),
new XElement("author", "Jon Galloway, Phil Haack, Brad Wilson, K. Scott Allen"),
new XElement("Price", "US $44.99")
)
);
Literal1.Text = singleXElement.ToString();
TextBox1.Text = multipleXElement.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>LINQ to XML (XElement) - How to create XML element</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
LINQ to XML (XElement) - How to create XML element
</h2>
<hr width="600" align="left" color="CornFlowerBlue" />
<asp:Literal
ID="Literal1"
runat="server"
Mode="Encode"
>
</asp:Literal>
<br /><br />
<asp:TextBox
ID="TextBox1"
runat="server"
TextMode="MultiLine"
Columns="85"
Rows="15"
Enabled="false"
>
</asp:TextBox>
<br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Create XElement"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>
- linq - How to determine if any items in a sequence match condition
- linq - How to determine if a sequence contains a specific element
- linq - Get a subset of elements from a sequence that match condition
- linq - How to concatenate two sequences
- linq - How to create a comment in an XML document
- linq - How to create an XML document
- linq - How to count occurrences of a string within a string
- linq - How to select distinct elements from a sequence
- LINQ to remove objects within a List
- linq - How to sort a date list order by ascending descending