开发者

Open XML - where did my Prefix Go?

开发者 https://www.devze.com 2023-01-04 18:48 出处:网络
While debugging, the newly created element has its Prefix as w: , but the Xmldoc at the end loses it.

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 = n开发者_JS百科ew 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;
}


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 :

        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;       
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消