Skip to content
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

XmlWriterSettings.NewLineOnAttributes is ignored on writing xmlns:* attributes #74840

Closed
smdn opened this issue Aug 30, 2022 · 3 comments · Fixed by #81822
Closed

XmlWriterSettings.NewLineOnAttributes is ignored on writing xmlns:* attributes #74840

smdn opened this issue Aug 30, 2022 · 3 comments · Fixed by #81822

Comments

@smdn
Copy link
Contributor

smdn commented Aug 30, 2022

Description

As described in the title.
XmlWriterSettings.NewLineOnAttributes is applied to normal attributes properly, but seems not to be applied to xmlns attributes.

This problem seems to occur with both case of xmlns="..." and xmlns:nsname="...".

Reproduction Steps

Code for reproduction:

using System.Xml;
using System.Xml.Linq;

var nsSvg = (XNamespace)"http://www.w3.org/2000/svg";
var nsXLink = (XNamespace)"http://www.w3.org/1999/xlink";
var doc = new XDocument(
  new XDeclaration("1.0", "utf-8", null),
  new XElement(
    nsSvg + "svg",
    new XAttribute("version", "1.1"),
    new XAttribute("width", "10"),
    new XAttribute(XNamespace.Xmlns + "xlink", nsXLink.NamespaceName),
    new XAttribute("height", "10"),
    new XElement(nsSvg + "g")
  )
);

var settings = new XmlWriterSettings() {
  CloseOutput = false,
  Indent = true,
  IndentChars = " ",
  NewLineChars = "\n",
  NewLineOnAttributes = true
};

using (var writer = XmlWriter.Create(Console.Out, settings)) {
  doc.Save(writer);
}

Console.WriteLine();

Note: The same problem occurs with XDocument created from XDocument.Load.

var reader = new StringReader(@"<?xml version=""1.0"" encoding=""utf-8""?>
<svg
 version=""1.1""
 xmlns=""http://www.w3.org/2000/svg""
 width=""10""
 xmlns:xlink=""http://www.w3.org/1999/xlink""
 height=""10""
>
 <g />
</svg>
");
var doc = XDocument.Load(reader);

Expected behavior

<?xml version="1.0" encoding="utf-8"?>
<svg
 version="1.1"
 width="10"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 height="10"
 xmlns="http://www.w3.org/2000/svg">
 <g />
</svg>

Actual behavior

<?xml version="1.0" encoding="utf-8"?>
<svg
 version="1.1"
 width="10" xmlns:xlink="http://www.w3.org/1999/xlink"
 height="10" xmlns="http://www.w3.org/2000/svg">
 <g />
</svg>

Regression?

No response

Known Workarounds

No response

Configuration

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.400
 Commit:    7771abd614

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.400/

global.json file:
  Not found

Host:
  Version:      6.0.8
  Architecture: x64
  Commit:       55fb7ef977

*snip*

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 30, 2022
@ghost
Copy link

ghost commented Aug 30, 2022

Tagging subscribers to this area: @dotnet/area-system-xml
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

As described in the title.
XmlWriterSettings.NewLineOnAttributes is applied to normal attributes properly, but seems not to be applied to xmlns attributes.

This problem seems to occur with both case of xmlns="..." and xmlns:nsname="...".

Reproduction Steps

Code for reproduction:

using System.Xml;
using System.Xml.Linq;

var nsSvg = (XNamespace)"http://www.w3.org/2000/svg";
var nsXLink = (XNamespace)"http://www.w3.org/1999/xlink";
var doc = new XDocument(
  new XDeclaration("1.0", "utf-8", null),
  new XElement(
    nsSvg + "svg",
    new XAttribute("version", "1.1"),
    new XAttribute("width", "10"),
    new XAttribute(XNamespace.Xmlns + "xlink", nsXLink.NamespaceName),
    new XAttribute("height", "10"),
    new XElement(nsSvg + "g")
  )
);

var settings = new XmlWriterSettings() {
  CloseOutput = false,
  Indent = true,
  IndentChars = " ",
  NewLineChars = "\n",
  NewLineOnAttributes = true
};

using (var writer = XmlWriter.Create(Console.Out, settings)) {
  doc.Save(writer);
}

Console.WriteLine();

Note: The same problem occurs with XDocument created from XDocument.Load.

var reader = new StringReader(@"<?xml version=""1.0"" encoding=""utf-8""?>
<svg
 version=""1.1""
 xmlns=""http://www.w3.org/2000/svg""
 width=""10""
 xmlns:xlink=""http://www.w3.org/1999/xlink""
 height=""10""
>
 <g />
</svg>
");
var doc = XDocument.Load(reader);

Expected behavior

<?xml version="1.0" encoding="utf-8"?>
<svg
 version="1.1"
 width="10"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 height="10"
 xmlns="http://www.w3.org/2000/svg">
 <g />
</svg>

Actual behavior

<?xml version="1.0" encoding="utf-8"?>
<svg
 version="1.1"
 width="10" xmlns:xlink="http://www.w3.org/1999/xlink"
 height="10" xmlns="http://www.w3.org/2000/svg">
 <g />
</svg>

Regression?

No response

Known Workarounds

No response

Configuration

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.400
 Commit:    7771abd614

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.400/

global.json file:
  Not found

Host:
  Version:      6.0.8
  Architecture: x64
  Commit:       55fb7ef977

*snip*

Other information

No response

Author: smdn
Assignees: -
Labels:

area-System.Xml

Milestone: -

@krwq krwq added bug and removed untriaged New issue has not been triaged by the area owner labels Aug 31, 2022
@krwq krwq added this to the Future milestone Aug 31, 2022
@krwq
Copy link
Member

krwq commented Aug 31, 2022

Hey @smdn, it does looks like a bug although since this behavior hasn't changed probably for at least 10 years we probably won't pick this up. I'll recommend to create a PR if you're interested in this. I'd also recommend starting out testing directly with XmlWriter since it's most likely where this originates from.

@smdn
Copy link
Contributor Author

smdn commented Aug 31, 2022

it does looks like a bug although since this behavior hasn't changed probably for at least 10 years we probably won't pick this up.

@krwq, thank you for telling me the background.

For now, I will try to handle this by creating custom XmlWriter or something.
After that, I might try to fix this.

Thanks!

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 8, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Feb 9, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants