Skip to content

Commit

Permalink
modernize snippet (#7907)
Browse files Browse the repository at this point in the history
gewarren authored Apr 4, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ea80920 commit 7536fe5
Showing 3 changed files with 57 additions and 122 deletions.
34 changes: 34 additions & 0 deletions snippets/csharp/System.Xml.XPath/Extensions/CreateNavigator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Xsl;

string xslMarkup = @"<?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>";

XDocument xmlTree = new(
new XElement("Parent",
new XElement("Child1", "Child1 data"),
new XElement("Child2", "Child2 data")
)
);

XDocument newTree = new();
using (XmlWriter writer = newTree.CreateWriter())
{
// Load the style sheet.
XslCompiledTransform xslt = new();
xslt.Load(XmlReader.Create(new StringReader(xslMarkup)));

// Execute the transform and output the results to a writer.
xslt.Transform(xmlTree.CreateNavigator(), writer);
}

Console.WriteLine(newTree);
9 changes: 9 additions & 0 deletions snippets/csharp/System.Xml.XPath/Extensions/project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
136 changes: 14 additions & 122 deletions xml/System.Xml.XPath/Extensions.xml
Original file line number Diff line number Diff line change
@@ -61,94 +61,20 @@
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
You cannot edit the XML tree by using the <xref:System.Xml.XPath.XPathNavigator> that is returned by this method. The <xref:System.Xml.XPath.XPathNavigator.CanEdit%2A> property returns `false`.
## Remarks
You cannot edit the XML tree by using the <xref:System.Xml.XPath.XPathNavigator> that's returned by this method. The <xref:System.Xml.XPath.XPathNavigator.CanEdit%2A> property returns `false`.
You cannot create an <xref:System.Xml.XPath.XPathNavigator> for a <xref:System.Xml.Linq.XDocumentType> node. Document types do not participate in the XPath data model.
Namespace declarations are reported from left to right. In contrast, for <xref:System.Xml.XmlDocument> namespaces are reported from right to left. This is conformant behavior because namespace declarations are not ordered in the XPath data model.
Namespace declarations are reported from left to right. In contrast, for <xref:System.Xml.XmlDocument>, namespaces are reported from right to left. This is conformant behavior because namespace declarations are not ordered in the XPath data model.
The method <xref:System.Xml.XPath.XPathNavigator.MoveToId%2A> is not supported for navigators that are returned by this method.
You can use this method to perform an XSLT transformation. You can create an XML tree, create an <xref:System.Xml.XPath.XPathNavigator> from the XML tree, create a new document, and create a <xref:System.Xml.XmlWriter> that will write into the new document. Then, you can invoke the XSLT transformation, passing the <xref:System.Xml.XPath.XPathNavigator> and <xref:System.Xml.XmlWriter> to the transformation. After the transformation successfully completes, the new XML tree is populated with the results of the transformation.
To perform an XSLT transformation, you can use either an <xref:System.Xml.XmlReader> or an <xref:System.Xml.XPath.XPathNavigator>. The two approaches have different performance characteristics. Some transformations will execute faster when using an <xref:System.Xml.XmlReader>, and others will execute faster when using a <xref:System.Xml.XPath.XPathNavigator>. If performance is a concern, we recommend that you experiment with each approach to determine which will perform better in your circumstances.
```csharp
string xslMarkup = @"<?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>";
XDocument xmlTree = new XDocument(
new XElement("Parent",
new XElement("Child1", "Child1 data"),
new XElement("Child2", "Child2 data")
)
);
XDocument newTree = new XDocument();
using (XmlWriter writer = newTree.CreateWriter()) {
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XmlReader.Create(new StringReader(xslMarkup)));
// Execute the transform and output the results to a writer.
xslt.Transform(xmlTree.CreateNavigator(), writer);
}
Console.WriteLine(newTree);
```
```vb
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 XDocument = <?xml version='1.0'?>
<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.CreateNavigator(), writer)
End Using
Console.WriteLine(newTree)
```
This example produces the following output:
```xml
<Root>
<C1>Child1 data</C1>
<C2>Child2 data</C2>
</Root>
```
To perform an XSLT transformation, you can use either an <xref:System.Xml.XmlReader> or an <xref:System.Xml.XPath.XPathNavigator>. The two approaches have different performance characteristics. Some transformations will execute faster when using an <xref:System.Xml.XmlReader>, and others will execute faster when using a <xref:System.Xml.XPath.XPathNavigator>. If performance is a concern, we recommend that you experiment with each approach to determine which will perform better in your circumstances.
]]></format>
</remarks>
</Docs>
@@ -212,47 +138,14 @@ Console.WriteLine(newTree)
You can use this method to perform an XSLT transformation. You can create an XML tree, create an <xref:System.Xml.XPath.XPathNavigator> from the XML tree, create a new document, and create a <xref:System.Xml.XmlWriter> that will write into the new document. Then, you can invoke the XSLT transformation, passing the <xref:System.Xml.XPath.XPathNavigator> and <xref:System.Xml.XmlWriter> to the transform. After the transformation successfully completes, the new XML tree is populated with the results of the transformation.
To perform an XSLT transformation, you can use either an <xref:System.Xml.XmlReader> or an <xref:System.Xml.XPath.XPathNavigator>. The two approaches have different performance characteristics. Some transformations will execute faster when using an <xref:System.Xml.XmlReader>, and others will execute faster when using a <xref:System.Xml.XPath.XPathNavigator>. If performance is a concern, we recommend that you experiment with each approach to determine which will perform better in your circumstances.
## Examples
```csharp
string xslMarkup = @"<?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>";
XDocument xmlTree = new XDocument(
new XElement("Parent",
new XElement("Child1", "Child1 data"),
new XElement("Child2", "Child2 data")
)
);
XDocument newTree = new XDocument();
using (XmlWriter writer = newTree.CreateWriter()) {
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XmlReader.Create(new StringReader(xslMarkup)));
// Execute the transform and output the results to a writer.
xslt.Transform(xmlTree.CreateNavigator(), writer);
}
Console.WriteLine(newTree);
```
To perform an XSLT transformation, you can use either an <xref:System.Xml.XmlReader> or an <xref:System.Xml.XPath.XPathNavigator>. The two approaches have different performance characteristics. Some transformations will execute faster when using an <xref:System.Xml.XmlReader>, and others will execute faster when using a <xref:System.Xml.XPath.XPathNavigator>. If performance is a concern, we recommend that you experiment with each approach to determine which will perform better in your circumstances.
## Examples
:::code language="csharp" source="~/snippets/csharp/System.Xml.XPath/Extensions/CreateNavigator.cs":::
```vb
Dim xslMarkup As XDocument = _
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'>
@@ -285,11 +178,10 @@ End Using
Console.WriteLine(newTree)
```
This example produces the following output:
This example produces the following output:
```xml
<Root>
<Root>
<C1>Child1 data</C1>
<C2>Child2 data</C2>
</Root>

0 comments on commit 7536fe5

Please sign in to comment.