You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
conststringxml="<test></test>";vardoc=newXmlDocument();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"varsb=newStringBuilder();usingXmlWriterxmlWriter=XmlWriter.Create(sb,newXmlWriterSettings(){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><testxmlns="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
The text was updated successfully, but these errors were encountered:
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.
conststringxml="<test></test>";vardoc=newXmlDocument();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");varpropInfo=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";varsb=newStringBuilder();usingXmlWriterxmlWriter=XmlWriter.Create(sb,newXmlWriterSettings(){Encoding=Encoding.Unicode,OmitXmlDeclaration=true,});doc.Save(xmlWriter);// Works now
Description
The
NamespaceURI
member ofXmlElement
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:
The problem also affects GetNamespaceOfPrefix() if you have this:
And then you delete the namespace declaration in
test
. The wayGetNamespaceOfPrefix()
works is if the element has no attributes, it uses the element'sNamespaceURI
if the prefixes are the same (which they are). ThereforeGetNamespaceOfPrefix()
continues to return 'ns' even though an attribute declaring that no longer exists.Expected behavior
I would expect that both
GetNamespaceOfPrefix()
andNamespaceURI
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 toSave()
it out and reload, which can also not be done.Actual behavior
NamespaceURI
is not updated when attributes change the namespace, and therefore sometimesGetNamespaceOfPrefix
also is not updated. This causesSave()
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
The text was updated successfully, but these errors were encountered: