LINQ To XML Samples - ConstructionOn This Page |
Construct an XElement from literal
Public Sub XLinq6()
Dim xml = _
<purchaseOrder price="100">
<item price="50">Motor</item>
<item price="50">Cable</item>
</purchaseOrder>
Console.WriteLine(xml)
End Sub
Result:
<purchaseOrder price="100">
<item price="50">Motor</item>
<item price="50">Cable</item>
</purchaseOrder>
Add XML declaration to a document
Public Sub XLinq7()
Dim doc = <?xml version="1.0" encoding="UTF-16" standalone="yes"?><foo/>
Dim sw = New StringWriter()
doc.Save(sw)
Console.WriteLine(sw)
End Sub
Result:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<foo />
Computed element name
Public Sub XLinq8()
Dim customers = XDocument.Load(dataPath + "nw_customers.xml")
Dim name As String = customers.<Root>.<Customers>.@CustomerID
Dim result As XElement = <<%= name %>>Element with a computed name</>
Console.WriteLine(result)
End Sub
Result:
<ALFKI>Element with a computed name</ALFKI>
Create a simple config file
Public Sub XLinq9()
Dim myDocument = <?xml version="1.0"?>
<configuration>
<system.web>
<membership>
<providers>
<add name="WebAdminMembershipProvider" type="System.Web.Administration.WebAdminMembershipProvider"/>
</providers>
</membership>
<httpModules>
<add name="WebAdminModule" type="System.Web.Administration.WebAdminModule"/>
</httpModules>
<authentication mode="Windows"/>
<authorization>
<deny users="?"/>
</authorization>
<identity impersonate="true"/>
<trust level="full"/>
<pages validationRequest="true"/>
</system.web>
</configuration>
Console.WriteLine(myDocument)
End Sub
Result:
<configuration>
<system.web>
<membership>
<providers>
<add name="WebAdminMembershipProvider" type="System.Web.Administration.WebAdminMembershipProvider" />
</providers>
</membership>
<httpModules>
<add name="WebAdminModule" type="System.Web.Administration.WebAdminModule" />
</httpModules>
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<identity impersonate="true" />
<trust level="full" />
<pages validationRequest="true" />
</system.web>
</configuration>
Create an XmlSchema
Public Sub XLinq10()
Dim result = _
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="root" sql:is-constant="1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Customers" minOccurs="100" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CompanyName" type="xsd:string"/>
<xsd:element name="ContactName" type="xsd:string"/>
<xsd:element name="ContactTitle" type="xsd:string"/>
<xsd:element name="Phone" type="xsd:string"/>
<xsd:element name="Fax" type="xsd:string"/>
<xsd:element ref="FullAddress" maxOccurs="3"/>
<xsd:element name="Date" type="xsd:date"/>
</xsd:sequence>
<xsd:attribute name="CustomerID" type="xsd:integer"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="FullAddress" sql:relation="Customers" sql:relationship="CustAdd" sql:key-fields="CustomerID">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Address" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"/>
<xsd:element name="Region" type="xsd:string"/>
<xsd:element name="PostalCode" type="xsd:string"/>
<xsd:element name="Country" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Console.WriteLine(result)
End Sub
Result:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="root" sql:is-constant="1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Customers" minOccurs="100" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CompanyName" type="xsd:string" />
<xsd:element name="ContactName" type="xsd:string" />
<xsd:element name="ContactTitle" type="xsd:string" />
<xsd:element name="Phone" type="xsd:string" />
<xsd:element name="Fax" type="xsd:string" />
<xsd:element ref="FullAddress" maxOccurs="3" />
<xsd:element name="Date" type="xsd:date" />
</xsd:sequence>
<xsd:attribute name="CustomerID" type="xsd:integer" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="FullAddress" sql:relation="Customers" sql:relationship="CustAdd" sql:key-fields="CustomerID">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Address" type="xsd:string" />
<xsd:element name="City" type="xsd:string" />
<xsd:element name="Region" type="xsd:string" />
<xsd:element name="PostalCode" type="xsd:string" />
<xsd:element name="Country" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Create an XML document with an XSLT PI
Public Sub XLinq11()
Dim result = <?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='diff.xsl'?>
<foo/>
Console.WriteLine(result)
End Sub
Result:
<?xml-stylesheet type='text/xsl' href='diff.xsl'?>
<foo />
XML comment construction
Public Sub XLinq12()
Dim result = <?xml version="1.0"?>
<!--My phone book-->
<phoneBook>
<!--My friends-->
<Contact name="Ralph">
<homephone>425-234-4567</homephone>
<cellphone>206-345-75656</cellphone>
</Contact>
<Contact name="Dave">
<homephone>516-756-9454</homephone>
<cellphone>516-762-1546</cellphone>
</Contact>
<!--My family-->
<Contact name="Julia">
<homephone>425-578-1053</homephone>
<cellphone></cellphone>
</Contact>
<!--My team-->
<Contact name="Robert">
<homephone>345-565-1653</homephone>
<cellphone>321-456-2567</cellphone>
</Contact>
</phoneBook>
Console.WriteLine(result)
End Sub
Result:
<!--My phone book-->
<phoneBook>
<!--My friends-->
<Contact name="Ralph">
<homephone>425-234-4567</homephone>
<cellphone>206-345-75656</cellphone>
</Contact>
<Contact name="Dave">
<homephone>516-756-9454</homephone>
<cellphone>516-762-1546</cellphone>
</Contact>
<!--My family-->
<Contact name="Julia">
<homephone>425-578-1053</homephone>
<cellphone></cellphone>
</Contact>
<!--My team-->
<Contact name="Robert">
<homephone>345-565-1653</homephone>
<cellphone>321-456-2567</cellphone>
</Contact>
</phoneBook>
Create a CData section
Public Sub XLinq13()
Dim e = <Dump><![CDATA[<dump>this is some xml</dump>]]>some other text</Dump>
Console.WriteLine("Element Value: " & e.Value)
Console.WriteLine("Text nodes collapsed!: " & e.Nodes(0).ToString)
Console.WriteLine("CData preserved on serialization: " & e.ToString)
End Sub
Result:
Element Value: <dump>this is some xml</dump>some other text
Text nodes collapsed!: <![CDATA[<dump>this is some xml</dump>]]>
CData preserved on serialization: <Dump><![CDATA[<dump>this is some xml</dump>]]>some other text</Dump>
Create a sequence of customer elements
Public Sub XLinq14()
Dim cSequence = <?xml version="1.0"?>
<root>
<customer id="x">new customer</customer>
<customer id="y">new customer</customer>
<customer id="z">new customer</customer>
</root>
For Each c In From cust In cSequence.<root>.<customer>
Console.WriteLine(c)
Next
End Sub
Result:
<customer id="x">new customer</customer>
<customer id="y">new customer</customer>
<customer id="z">new customer</customer>