【发布时间】:2023-04-10 20:55:01
【问题描述】:
我正在使用this code 将 XElement 转换为 OpenXmlElement
internal static OpenXmlElement ToOpenXml(this XElement xel)
{
using (var sw = new StreamWriter(new MemoryStream()))
{
sw.Write(xel.ToString());
sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.Begin);
var re = OpenXmlReader.Create(sw.BaseStream);
re.Read();
var oxe = re.LoadCurrentElement();
re.Close();
return oxe;
}
}
在转换之前我有一个 XElement
<w:ind w:firstLine="0" w:left="0" w:right="0"/>
转换后的样子
<w:ind w:firstLine="0" w:end="0" w:start="0"/>
然后,此元素使用以下内容无法通过 OpenXml 验证
var v = new OpenXmlValidator();
var errs = v.Validate(doc);
报告错误:
Description="The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:start' attribute is not declared."
Description="The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:end' attribute is not declared."
我是否需要做其他事情来将这些属性添加到架构中,或者我是否需要找到一种新的方法来从 XElement 转换为 OpenXml?
我正在使用 nuget 包 DocumentFormat.OpenXml 版本 2.9.1(最新)。
编辑:查看the OpenXml standard,似乎应该识别左/开始和右/结束,这表明 OpenXmlValidator 不太正确。大概我可以忽略那些验证错误?
很多感谢
【问题讨论】:
-
您能否提供有关您传递给
ToOpenXml方法的XElement的更多信息?传递w:indXElement会产生OpenXmlUnknownElement并且属性不会更改。传递w:documentXElement会产生Document并且Indentation后代的属性会按照您的描述进行更改。
标签: c# linq-to-xml openxml