Training
Module
Work with XMLports in Dynamics 365 Business Central - Training
Learn how to define and use XMLports in AL, understand different nodes and properties, and apply them in AL code.
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 use XSLT to transform an XML tree, using XmlReader to read and XmlWriter to write.
This example creates an XML tree and uses XSLT to transform it. It makes use of an XmlReader to read the original tree, and an XmlWriter to write the transformed version.
It starts by creating:
It then invokes an XSLT transformation that uses the XmlReader to read the original XML tree, and the XmlWriter to write the transformed tree to the new document.
string xslt = @"<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:template match='/Parent'>
<Root>
<C1>
<xsl:value-of select='Child1'/>
</C1>
<C2>
<xsl:value-of select='Child2'/>
</C2>
</Root>
</xsl:template>
</xsl:stylesheet>";
var oldDocument = new XDocument(
new XElement("Parent",
new XElement("Child1", "Child1 data"),
new XElement("Child2", "Child2 data")
)
);
var newDocument = new XDocument();
using (var stringReader = new StringReader(xslt))
{
using (XmlReader xsltReader = XmlReader.Create(stringReader))
{
var transformer = new XslCompiledTransform();
transformer.Load(xsltReader);
using (XmlReader oldDocumentReader = oldDocument.CreateReader())
{
using (XmlWriter newDocumentWriter = newDocument.CreateWriter())
{
transformer.Transform(oldDocumentReader, newDocumentWriter);
}
}
}
}
string result = newDocument.ToString();
Console.WriteLine(result);
Dim xslMarkup As XDocument = _
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:template match='/Parent'>
<Root>
<C1>
<xsl:value-of select='Child1'/>
</C1>
<C2>
<xsl:value-of select='Child2'/>
</C2>
</Root>
</xsl:template>
</xsl:stylesheet>
Dim xmlTree As XElement = _
<Parent>
<Child1>Child1 data</Child1>
<Child2>Child2 data</Child2>
</Parent>
Dim newTree As XDocument = New XDocument()
Using writer As XmlWriter = newTree.CreateWriter()
' Load the style sheet.
Dim xslt As XslCompiledTransform = _
New XslCompiledTransform()
xslt.Load(xslMarkup.CreateReader())
' Execute the transform and output the results to a writer.
xslt.Transform(xmlTree.CreateReader(), writer)
End Using
Console.WriteLine(newTree)
This example produces the following output:
<Root>
<C1>Child1 data</C1>
<C2>Child2 data</C2>
</Root>
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Work with XMLports in Dynamics 365 Business Central - Training
Learn how to define and use XMLports in AL, understand different nodes and properties, and apply them in AL code.