-
Notifications
You must be signed in to change notification settings - Fork 477
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
Conversation
Looks good to me. @H1Gdev, @wieslawsoltes - can one of you have another look please? |
I will check this PR. |
There was a problem hiding this 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
toXmlWriter
. - change from
new XmlTextWriter()
toXmlWriter.Create()
. - When adding
SvgDocument
element, also specify SVG namespace. - Change namespace specification of attributes.
Source/SvgNamespace.cs
Outdated
@@ -0,0 +1,11 @@ | |||
using System; | |||
using System.Collections.Generic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary using
.
Source/SvgDocument.cs
Outdated
@@ -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 |
There was a problem hiding this comment.
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 ?
Source/SvgExtentions.cs
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use ?
Source/SvgElementFactory.cs
Outdated
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix #702.
Source/SvgElementFactory.cs
Outdated
@@ -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); |
There was a problem hiding this comment.
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 ?
What is "Issue #2786" ? |
@bbsgfalconer Please separate PRs for each issue. |
Oops - tidying up my branches. Well separate into smaller changes and resubmit. |
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:
See also conformance options based on ConformanceLevel
Any other comments?
The changes impacted SvgAttributeAttribute.cs such that:
Since the SvgNamespace is needed more widely across SvgElement and SvgAttributeAttribute, I extracted it into its own static class.
All unit tests pass.