Open XML - 我的前缀去了哪里?
调试时,新创建的元素的前缀为 w: ,但最后的 Xmldoc 丢失了它。
下面元素的最终 InnerXML 为:
预期结果为
private static XmlDocument prepareHTMLChunks(PackagePart partDocumentXML)
{
XmlDocument doc = new XmlDocument();
Stream xmlStream = partDocumentXML.GetStream();
doc.Load(xmlStream);
NameTable nt = new NameTable();
nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
XmlNodeList nodeList = doc.SelectNodes("//w:bookmarkStart", nsManager);
foreach (XmlNode node in nodeList)
{
foreach (XmlAttribute attr in node.Attributes)
{
if (attr.Value.EndsWith("HTML"))
{
//XmlElement el = doc.CreateElement("w","w:altChunk",string.Empty);
XmlElement el = doc.CreateElement("altChunk",nsManager.LookupNamespace("w"));
XmlAttribute altChunkAttr = doc.CreateAttribute("r","id",string.Empty);
altChunkAttr.Prefix = "r";
altChunkAttr.Value = attr.Value;
el.SetAttributeNode(altChunkAttr);
XmlNode nodeToReplace = node.ParentNode;
XmlNode bodyNode = doc.SelectSingleNode("//w:body", nsManager);
bodyNode.ReplaceChild(el, nodeToReplace);
}
}
}
return doc;
}
While debugging, the newly created element has its Prefix as w: , but the Xmldoc at the end loses it.
The resultant InnerXML for the Element below is:<altChunk id="FF_HTML" xmlns="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
Expected Result is <w:altChunk r:id="FF_HTML"/>
private static XmlDocument prepareHTMLChunks(PackagePart partDocumentXML)
{
XmlDocument doc = new XmlDocument();
Stream xmlStream = partDocumentXML.GetStream();
doc.Load(xmlStream);
NameTable nt = new NameTable();
nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
XmlNodeList nodeList = doc.SelectNodes("//w:bookmarkStart", nsManager);
foreach (XmlNode node in nodeList)
{
foreach (XmlAttribute attr in node.Attributes)
{
if (attr.Value.EndsWith("HTML"))
{
//XmlElement el = doc.CreateElement("w","w:altChunk",string.Empty);
XmlElement el = doc.CreateElement("altChunk",nsManager.LookupNamespace("w"));
XmlAttribute altChunkAttr = doc.CreateAttribute("r","id",string.Empty);
altChunkAttr.Prefix = "r";
altChunkAttr.Value = attr.Value;
el.SetAttributeNode(altChunkAttr);
XmlNode nodeToReplace = node.ParentNode;
XmlNode bodyNode = doc.SelectSingleNode("//w:body", nsManager);
bodyNode.ReplaceChild(el, nodeToReplace);
}
}
}
return doc;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
前缀只是命名空间的别名。前缀本身并不重要。如果您愿意,您可以使用“prefix”作为前缀,它的含义完全相同。
同样,完全相同的结果可以来自您在问题中显示的 xmlns="..." 。它的含义与“w:”前缀完全相同,假设“w”是同一名称空间的别名。
The prefix is only an alias for the namespace. The prefix itself does not matter. You could use "prefix" as the prefix if you wanted, and it would mean exactly the same thing.
Similarly, the exact same result can come from the xmlns="..." that you show in your question. It means the exact same thing as with the "w:" prefix, assuming that "w" was aliased to the same namespace.
好的,我修好了:
Ok, I fixed it :