XmlElement.SetAttribute does not update element's NamespaceURI
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- backend-api-design
Research direction
Start with the XmlDocument and XmlElement reproduction in the issue, running it on .NET 9 to observe NamespaceURI, GetNamespaceOfPrefix(), and Save(). Trace the XmlElement attribute and namespace-handling entry points used by SetAttribute and namespace deletion. Done means namespace state remains consistent and the document can be saved and reloaded without the reported failure.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·