When the root element is an unknown element, OpenXmlPartReader could not get the correct values of LocalName, NamespaceUri and Prefix properties
Describe the bug In the CreateElement method of OpenXmlPartReader, when the element is unknown, it returns a new instance of OpenXmlUnknownElement but does not pass the qualified name and prefix parameters to its constructor, resulting in the reader being unable to retrieve the correct values of some properties such as LocalName
Observed behavior When the root element is an unknown element, the LocalName, NamepaceUri and Prefix properties of the reader are string.Empty
Expected behavior The reader returns the correct values of the element
Additional context It seems that even for the construction of known elements, the relevant parameters are not passed. If I use a different namespace prefix, the reader's Prefix property still returns the element's default prefix, not the actual value. Is this also a bug?
Hi @ElegantCat-nocat thank you for your request. I just want to clarify few things before I can proceed and troubleshoot your request. Do you mean WriteElement in the OpenXmlPartWriter?. OpenXmlPartReader doesn't have CreateElement method. Could you please provide some repro steps if possible. Thanks
Hi @ElegantCat-nocat thank you for your request. I just want to clarify few things before I can proceed and troubleshoot your request. Do you mean WriteElement in the OpenXmlPartWriter?. OpenXmlPartReader doesn't have CreateElement method. Could you please provide some repro steps if possible. Thanks
To @mkaszewiak No, I am referring to OpenXmlPartReader, its CreateElement method is located in src/DocumentFormat.OpenXml.Framework/OpenXmlPartReader.cs, line 760.
You can use the following code to reproduce the behavior:
using var document = SpreadsheetDocument.Create(@"example.xlsx", SpreadsheetDocumentType.Workbook);
var extendedPart = document.AddExtendedPart(
"http://www.example.com/",
"application/xml",
".xml");
// Write some custom elements which are unknown to the OpenXmlPartReader
using (var writer = new StreamWriter(extendedPart.GetStream(FileMode.Create)))
{
writer.Write(
"""
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a:root xmlns:a="http://www.example.com/">
<a:child />
</a:root>
""");
}
OpenXmlReader reader = OpenXmlReader.Create(extendedPart);
while (reader.Read())
{
Console.WriteLine($"{reader.Prefix}, {reader.LocalName}, {reader.NamespaceUri}");
}
I got the following output:
, ,
a, child, http://www.example.com/
a, child, http://www.example.com/
, ,
If I use XmlReader, I can get the correct values of the root element:
XmlReader reader = XmlReader.Create(
extendedPart.GetStream(FileMode.Open),
new() { CloseInput = true, IgnoreWhitespace = true });
reader.Read(); // skip xml declaration
while (reader.Read())
{
Console.WriteLine($"{reader.Prefix}, {reader.LocalName}, {reader.NamespaceURI}");
}
a, root, http://www.example.com/
a, child, http://www.example.com/
a, root, http://www.example.com/
Hi @ElegantCat-nocat thanks for the clarification and repro steps. Thanks for the clarification and the repro steps. I’ll look into the issue.