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