XmlElement.SetAttribute does not update element's NamespaceURI

Open
#111,738 3 comments 0 reactions 0 assignees View on GitHub

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

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

area-System.Xml
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.