views:

35

answers:

2

Hi everyone. Please help with this if you can. Its very urgent and I can't seem to get my head around the problem. Note that I'm not a regular OpenXML developer. 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"/>

Any help please?

       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;
    }
+1  A: 

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.

John Saunders
I understsand your answer John, but how do I get the expected result : <w:altChunk r:id="FF_HTML"/>and not : <altChunk id="FF_HTML"/>The problem is that without the prefixes in the XML ,word doesn't open the doc as you might imagine.Thanks Fox
Fox
@Fox: what version of Word are you using, and please post some XML you say will not open without the prefix. I'd be extremely shocked to find that Word doesn't obey basic XML standards that have been in place for over a decade.
John Saunders
It all lies with the prefixes Using Word 2007My Method above renders the XML Element as <altChunk id="FF_HTML"/>while Word expects <w:altChunk p:id="FF_HTML"/>. So the only thing I'm trying to figure out is from my method above, how to get the prefixes in the XML when rendering.
Fox
@Fox: I'm hoping you're wrong. If you're right, then Word has a critical bug, and should be immediately fixed.
John Saunders
Hey John. I actually managed to fix the issue. When inserting the XML, i was missing the schema references.. By doing the following, it all worked out : 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");
Fox
Thanks for your assitance though
Fox
@Fox: experiment: try it with the namespaces, but without the prefixes.
John Saunders
A: 

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