Open XML - 我的前缀去了哪里?

发布于 2024-09-06 21:46:25 字数 1657 浏览 6 评论 0原文

调试时,新创建的元素的前缀为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-09-13 21:46:25

前缀只是命名空间的别名。前缀本身并不重要。如果您愿意,您可以使用“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.

梦情居士 2024-09-13 21:46:25

好的,我修好了:

        private static XmlDocument prepareHTMLChunks(PackagePart partDocumentXML)
    {

        XmlDocument _document = new XmlDocument();
        Stream xmlStream = partDocumentXML.GetStream();
        _document.Load(xmlStream);

        XmlNamespaceManager _ns = new XmlNamespaceManager(_document.NameTable);
        _ns.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

        XmlNode _body = _document.SelectSingleNode("//w:body", _ns);

        foreach (XmlNode _node in _document.SelectNodes("//w:bookmarkStart", _ns))
        {
            foreach (XmlAttribute _attribute in _node.Attributes)
            {
                if (_attribute.Value.EndsWith("HTML"))
                {
                    XmlElement _element = _document.CreateElement("w", "altChunk", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                    XmlAttribute _newAttr = _document.CreateAttribute("r", "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");                        
                    _newAttr.Value = _attribute.Value;
                    _element.Attributes.Append(_newAttr);
                    _body.AppendChild(_element);                        
                }           
            }
        }            
        return _document;       
    }

Ok, I fixed it :

        private static XmlDocument prepareHTMLChunks(PackagePart partDocumentXML)
    {

        XmlDocument _document = new XmlDocument();
        Stream xmlStream = partDocumentXML.GetStream();
        _document.Load(xmlStream);

        XmlNamespaceManager _ns = new XmlNamespaceManager(_document.NameTable);
        _ns.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

        XmlNode _body = _document.SelectSingleNode("//w:body", _ns);

        foreach (XmlNode _node in _document.SelectNodes("//w:bookmarkStart", _ns))
        {
            foreach (XmlAttribute _attribute in _node.Attributes)
            {
                if (_attribute.Value.EndsWith("HTML"))
                {
                    XmlElement _element = _document.CreateElement("w", "altChunk", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                    XmlAttribute _newAttr = _document.CreateAttribute("r", "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");                        
                    _newAttr.Value = _attribute.Value;
                    _element.Attributes.Append(_newAttr);
                    _body.AppendChild(_element);                        
                }           
            }
        }            
        return _document;       
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文