我正在研究通常非常好的开源项目Excel Data Reader中的一个神秘错误。它正在跳过从我的特定 OpenXML .xlsx 电子表格中读取的值。
问题出现在ReadSheetRow 方法中(下面的演示代码)。源 XML 由 Excel 保存并且不包含出现奇怪行为的空格。然而,已经用空格重新格式化的 XML(例如在 Visual Studio 中转到编辑、高级、格式化文档)工作得很好!
带空格的测试数据:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<sheetData>
<row r="5" spans="1:73" s="7" customFormat="1">
<c r="B5" s="12">
<v>39844</v>
</c>
<c r="C5" s="8"/>
<c r="D5" s="8"/>
<c r="E5" s="8"/>
<c r="F5" s="8"/>
<c r="G5" s="8"/>
<c r="H5" s="12">
<v>39872</v>
</c>
<c r="I5" s="8"/>
<c r="J5" s="8"/>
<c r="K5" s="8"/>
<c r="L5" s="8"/>
<c r="M5" s="8"/>
<c r="N5" s="12">
<v>39903</v>
</c>
</row>
</sheetData>
</worksheet>
没有空格的测试数据:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData><row r="5" spans="1:73" s="7" customFormat="1"><c r="B5" s="12"><v>39844</v></c><c r="C5" s="8"/><c r="D5" s="8"/><c r="E5" s="8"/><c r="F5" s="8"/><c r="G5" s="8"/><c r="H5" s="12"><v>39872</v></c><c r="I5" s="8"/><c r="J5" s="8"/><c r="K5" s="8"/><c r="L5" s="8"/><c r="M5" s="8"/><c r="N5" s="12"><v>39903</v></c></row></sheetData></worksheet>
演示问题的示例代码:
请注意,A之后输出_xmlReader.Read()
,B之后输出ReadToDescendant
,C之后输出ReadElementContentAsObject
。
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.Whitespace) outStream.WriteLine(String.Format("*A* NodeType: {0}, Name: '{1}', Empty: {2}, Value: '{3}'", reader.NodeType, reader.Name, reader.IsEmptyElement, reader.Value));
if (reader.NodeType == XmlNodeType.Element && reader.Name == "c")
{
string a_s = reader.GetAttribute("s");
string a_t = reader.GetAttribute("t");
string a_r = reader.GetAttribute("r");
bool matchingDescendantFound = reader.ReadToDescendant("v");
if (reader.NodeType != XmlNodeType.Whitespace) outStream.WriteLine(String.Format("*B* NodeType: {0}, Name: '{1}', Empty: {2}, Value: '{3}'", reader.NodeType, reader.Name, reader.IsEmptyElement, reader.Value));
object o = reader.ReadElementContentAsObject();
if (reader.NodeType != XmlNodeType.Whitespace) outStream.WriteLine(String.Format("*C* NodeType: {0}, Name: '{1}', Empty: {2}, Value: '{3}'", reader.NodeType, reader.Name, reader.IsEmptyElement, reader.Value));
}
}
带有空格的 XML 的测试结果:
*A* 节点类型:XmlDeclaration,名称:'xml',空:False,值:'version="1.0" encoding="UTF-8" Standalone="yes"' *A* NodeType:元素,名称:'工作表',空:False,值:'' *A* 节点类型:元素,名称:'sheetData',空:False,值:'' *A* NodeType:元素,名称:'row',空:False,值:'' *A* 节点类型:元素,名称:'c',空:False,值:'' *B* 节点类型:元素,名称:'v',空:False,值:'' *A* 节点类型:EndElement,名称:'c',空:False,值:'' *A* 节点类型:元素,名称:'c',空:真,值:'' *B* 节点类型:元素,名称:'c',空:真,值:'' ...
没有空格的 XML 的测试结果:
*A* 节点类型:XmlDeclaration,名称:'xml',空:False,值:'version="1.0" encoding="UTF-8" Standalone="yes"' *A* NodeType:元素,名称:'工作表',空:False,值:'' *A* 节点类型:元素,名称:'sheetData',空:False,值:'' *A* NodeType:元素,名称:'row',空:False,值:'' *A* 节点类型:元素,名称:'c',空:False,值:'' *B* 节点类型:元素,名称:'v',空:False,值:'' *C* 节点类型:EndElement,名称:'c',空:False,值:'' *A* 节点类型:元素,名称:'c',空:真,值:'' *B* 节点类型:元素,名称:'c',空:真,值:'' ...
模式更改表明XmlReader 移动到ReadElementContentAsObject
的位置或可能的位置存在问题。ReadToDescendant
有谁知道这里可能会发生什么?