fix prefix dupplication error#155
Conversation
When using XmlService.Save, the prefix "p" may be duplicated.
|
I think this build failure is bacause of it. |
|
Thanks for submitting the PR! This is great. There's a few things.. would you be able to write it without having a capturing lambda or enumerable (foreach)? Also, a test would be great to have to ensure this doesn't break in the future. |
|
.. as for the build error, yes if you know what is needed to be done to fix It please update. I'll probably update this to use .NET 5 soon anyway. |
I can't come up with a way to write without 'Any' method nor enumerable. public string AddNamespace (string ns)
{
var l = Namespaces;
string prefix, s;
...
else
{
s = sctx.GetPreferredPrefix(ns);
if (!l.Any(i => i.Prefix == s))
prefix = s;
else
{
int getSuffix(string p)
=> !p.StartsWith("p") ? 0 :
int.TryParse(p.Substring(1), out var i) ? i :
1;
var idx = l.Max(i => getSuffix(i.Prefix)) + 1;
prefix = s + idx;
}
}
...
} |
|
How about creating a function: After looking into that method, I realize there's another use of an Any() with capturing lambda in that method too... There's no reason we couldn't make it more optimized while we notice it, I hope you don't mind. Also, just a quick question.. why do you start at 2? Does System.Xaml behave this way? |
|
When I checked System.Xaml, it started from 1. By the way, System.Xaml uses the acronyms, not the ‘p’ (for example, the demo code uses ‘ab’, ‘ab1’, ‘ab2’).
|
Reflects the review, Add test case.
|
@cwensley Additionary, I added workaround build.yml to avoid build failure. |
If there are some namespaces of the same acronym,
XamlServices.Savecause XmlException with the message "'xmlns:p' is a duplicate attribute name".Demonstration code
This pull request avoids the above problem by adding an ordinal number at the end