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

XmlElement.SetAttribute does not update element's NamespaceURI #111738

Open
Agendum opened this issue Jan 23, 2025 · 2 comments
Open

XmlElement.SetAttribute does not update element's NamespaceURI #111738

Agendum opened this issue Jan 23, 2025 · 2 comments
Labels
area-System.Xml untriaged New issue has not been triaged by the area owner

Comments

@Agendum
Copy link

Agendum commented Jan 23, 2025

Description

The NamespaceURI member of XmlElement is not updated when changing the namespace information in attributes. There also does not seem to be a way to resync the property after changing namespace information, and you cannot even save and reload the XML to get it because saving fails. Basically, there is nothing that can be done.

Reproduction Steps

Here is a small code snippet which demonstrates the problem:

const string xml = "<test></test>";
var doc = new XmlDocument();
doc.LoadXml(xml);
Console.WriteLine(doc.DocumentElement.NamespaceURI); // yields ""
Console.WriteLine(doc.DocumentElement.GetNamespaceOfPrefix("")); // yields ""
doc.DocumentElement.SetAttribute("xmlns", "http://www.w3.org/2000/xmlns/", "ns");
Console.WriteLine(doc.DocumentElement.NamespaceURI); // BUG: yields "", SHOULD BE "ns"
Console.WriteLine(doc.DocumentElement.GetNamespaceOfPrefix("")); // yields "ns"
var sb = new StringBuilder();
using XmlWriter xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings()
{
    Encoding = Encoding.Unicode,
    OmitXmlDeclaration = true,
});
doc.Save(xmlWriter); // BUG: throws because element namespace URI doesn't match attribute

The problem also affects GetNamespaceOfPrefix() if you have this:

<root><test xmlns="ns"></test></root>

And then you delete the namespace declaration in test. The way GetNamespaceOfPrefix() works is if the element has no attributes, it uses the element's NamespaceURI if the prefixes are the same (which they are). Therefore GetNamespaceOfPrefix() continues to return 'ns' even though an attribute declaring that no longer exists.

Expected behavior

I would expect that both GetNamespaceOfPrefix() and NamespaceURI would always reflect the actual prefix and namespace state of attributes on the element or in the ancestry. Even if they weren't reflected, I would expect to be able to Save() it out and reload, which can also not be done.

Actual behavior

NamespaceURI is not updated when attributes change the namespace, and therefore sometimes GetNamespaceOfPrefix also is not updated. This causes Save() to fail as well.

Regression?

I don't know.

Known Workarounds

I have not found any yet. I am interested in any workarounds, hopefully any which do not involve deleting and recreating the element.

Configuration

Windows 11, .NET 9, x64. I highly doubt this is specific to any configuration.

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 23, 2025
Copy link
Contributor

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

@Agendum
Copy link
Author

Agendum commented Jan 23, 2025

I have a work-around now, but it involves reflection, so it is not pretty. I am not endorsing anybody do this, but it illustrates that the internal XmlName of the element is not being updated when attributes change on the element, and even if that is by design, there is zero tooling around allowing somebody to do it.

const string xml = "<test></test>";
var doc = new XmlDocument();
doc.LoadXml(xml);
Console.WriteLine(doc.DocumentElement.NamespaceURI); // yields "";
Console.WriteLine(doc.DocumentElement.GetNamespaceOfPrefix("")); // yields "";
doc.DocumentElement.SetAttribute("xmlns", "http://www.w3.org/2000/xmlns/", "ns");

var propInfo = doc.DocumentElement
    .GetType()
    .GetProperty("XmlName", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
propInfo.SetValue(
    doc.DocumentElement,
    propInfo.GetValue(doc.CreateElement(doc.DocumentElement.Prefix, doc.DocumentElement.LocalName, "ns")));

Console.WriteLine(doc.DocumentElement.NamespaceURI); // yields "ns";
Console.WriteLine(doc.DocumentElement.GetNamespaceOfPrefix("")); // yields "ns";
var sb = new StringBuilder();
using XmlWriter xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings()
{
    Encoding = Encoding.Unicode,
    OmitXmlDeclaration = true,
});
doc.Save(xmlWriter); // Works now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Xml untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

1 participant