|
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
De shared Parse method van de XElement en XDocument klasse kan men gebruiken om deze op te vullen met XML informatie uit een String. 28.14.1. XElement.Parse MethodVisual Basic 2010 Broncode Option Explicit OnOption Strict OnImports System.Xml.Linq Namespace LoadingXML Class LoadingXElementFromStringExample Public Shared Sub Main() Dim inner As XElement = XElement.Parse( "<name>John Smith</name>") Dim outer As XElement = <employee><%= inner %></employee> Console.WriteLine(outer.ToString()) Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output <employee>
<name>John Smith</name>
</employee> boven
28.14.2. XDocument.Parse MethodEen voorbeeld met de XDocument.Parse method : Visual Basic 2010 Broncode Namespace LoadingXML Class LoadingXDocumentFromStringExample1 Public Shared Sub Main() Dim document As XDocument = _ XDocument.Parse( "<?xml version=""1.0""?>" & _ "<employee>" & _ " <name>John Smith</name>" & _ "</employee>") Console.WriteLine(document.ToString()) Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output <employee>
<name>John Smith</name>
</employee> Indien de String foutief is samengesteld, bijvoorbeeld bij fouten tegen de XML 1.0 specificaties, zoals het gebrek aan een root element, zal een System.Xml.XmlException optreden : Visual Basic 2010 Broncode Namespace LoadingXML Class LoadingXDocumentFromStringExample2 Public Shared Sub Main() Try Dim document As XDocument = _ XDocument.Parse( "<?xml version=""1.0""?>") Catch ex As System.Xml. XmlException Console.WriteLine(ex.Message) End Try Console.ReadLine() End Sub End ClassEnd NamespaceDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output Root element is missing.
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
|