Skip to content

Issue #2786 Amend SVG library to use XmlWriter instead of XmlTextWriter. #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from

Conversation

xprl-gjf
Copy link
Contributor

Reference Issue

Enhancement #693

What does this implement/fix? Explain your changes.

Generalize XML output to use an XmlWriter instead of the more specific XmlTextWriter. In particular, my use case is that I want to use my own custom XmlWriter to send output as part of an Asp.Net Blazor application.

The changes to the various WriteXXX(...) methods are trivial. However, the XmlWriter class performs some stricter checks than the XmlTextWriter and so I had to change the handling of namespaces. The existing code specifies namespace simply by writing an "xmlns" attribute. However, with XmlWriter, this causes an exception to be thrown (similar to this question on StackOverflow )

Note also the Microsoft documentation for XmlTextWriter:

Starting with the .NET Framework 2.0, we recommend that you use the XmlWriter class instead.

Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

See also conformance options based on ConformanceLevel

Any other comments?

The changes impacted SvgAttributeAttribute.cs such that:

  • the SVG namespace (http://www.w3.org/2000/svg) is no longer included in the list of namespaces that would be written into the root SvgFragment element from this list. Instead, all SvgElements are explicitly using this namespace as per SvgElement.cs
  • the 'NamespaceAndName' method is no longer needed. Instead, XmlWriter.WriteAttributeString is called with the namespace and prefix to handle this. (again, in SvgElement.cs)

Since the SvgNamespace is needed more widely across SvgElement and SvgAttributeAttribute, I extracted it into its own static class.

All unit tests pass.

@mrbean-bremen
Copy link
Member

Looks good to me. @H1Gdev, @wieslawsoltes - can one of you have another look please?

@H1Gdev
Copy link
Contributor

H1Gdev commented Mar 27, 2020

@mrbean-bremen

I will check this PR.

Copy link
Contributor

@H1Gdev H1Gdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For PR to change from XmlTextWriter to XmlWriter, I think there are many changes.

I think the following changes.

  • change from XmlTextWriter to XmlWriter.
  • change from new XmlTextWriter() to XmlWriter.Create().
  • When adding SvgDocument element, also specify SVG namespace.
  • Change namespace specification of attributes.

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary using.

@@ -727,10 +727,11 @@ public override void Write(XmlTextWriter writer)
public void Write(Stream stream, bool useBom = true)
{

var xmlWriter = new XmlTextWriter(stream, useBom ? Encoding.UTF8 : new System.Text.UTF8Encoding(false))
var xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use Create method, it should be enclosed in using ?

In case of XmlTextWriter, Stream is also disposed by Dispose method, but not in case of Create method ?

@@ -54,12 +54,15 @@ public static string GetXML(this SvgElement elem)
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
using (StringWriter str = new StringWriter())
{
using (XmlTextWriter xml = new XmlTextWriter(str))
var writerSettings = new XmlWriterSettings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use ?

if (reader.LocalName.Equals("style") && !(element is NonSvgElement))
if (reader.LocalName.Equals("xmlns"))
{
// namespace should already have been processed upon reading the element; do nothing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run a test or something like that and actually process this block ?

@@ -298,16 +293,14 @@ public override SvgElement DeepCopy<T>()
}

// Override the default behavior, writing out the namespaces.
protected override void WriteStartElement(XmlTextWriter writer)
protected override void WriteStartElement(XmlWriter writer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also adds namespace to inner SVG.(existing issue...)
This process should be in SvgDocument.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #702.

@@ -113,7 +113,7 @@ private SvgElement CreateElement<T>(XmlReader reader, bool fragmentIsDocument, S
else
{
// All non svg element (html, ...)
createdElement = new NonSvgElement(elementName);
createdElement = new NonSvgElement(elementName, elementNS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does SVG embedded in HTML have an unnecessary namespace ?

@H1Gdev
Copy link
Contributor

H1Gdev commented Mar 30, 2020

What is "Issue #2786" ?

@H1Gdev
Copy link
Contributor

H1Gdev commented Apr 2, 2020

@bbsgfalconer

Please separate PRs for each issue.

@xprl-gjf
Copy link
Contributor Author

xprl-gjf commented Apr 2, 2020

Oops - tidying up my branches. Well separate into smaller changes and resubmit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants