Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打开XML-我的前缀到哪里去了?_Xml_Xsd_Openxml - Fatal编程技术网

打开XML-我的前缀到哪里去了?

打开XML-我的前缀到哪里去了?,xml,xsd,openxml,Xml,Xsd,Openxml,调试时,新创建的元素的前缀为w:,但最后的Xmldoc会丢失它 以下元素的结果InnerXML为: 预期结果是 前缀只是名称空间的别名。前缀本身并不重要。如果你愿意,你可以用“prefix”作为前缀,它的意思完全一样 同样,您在问题中显示的xmlns=“…”也可以得出完全相同的结果。它的意思与“w:”前缀完全相同,假设“w”别名为同一名称空间。好的,我修复了它: private static XmlDocument prepareHTMLChunks(PackagePart p

调试时,新创建的元素的前缀为w:,但最后的Xmldoc会丢失它

以下元素的结果InnerXML为:

预期结果是


前缀只是名称空间的别名。前缀本身并不重要。如果你愿意,你可以用“prefix”作为前缀,它的意思完全一样

同样,您在问题中显示的xmlns=“…”也可以得出完全相同的结果。它的意思与“w:”前缀完全相同,假设“w”别名为同一名称空间。

好的,我修复了它:

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

约翰,我理解你的答案,但我如何得到预期的结果:而不是:问题是,如果XML中没有前缀,word就不会像你想象的那样打开文档。谢谢Fox@Fox:您使用的是什么版本的Word,请发布一些您认为没有前缀将无法打开的XML。我会非常震惊地发现Word没有遵守已经实施了十多年的基本XML标准。这一切都取决于使用Word 2007的前缀。我上面的方法按照Word的预期呈现XML元素。所以我唯一想弄明白的是,从上面的方法中,如何在呈现XML时获取前缀。@Fox:我希望你是错的。如果你是对的,那么Word有一个严重的bug,应该立即修复。嘿,约翰。我实际上解决了这个问题。插入XML时,我缺少模式引用。。通过执行以下操作,一切都得到了解决:XmlElement(元素=)document.CreateElement(“w”,“altChunk”,”);XmlAttribute(新属性=)document.CreateAttribute(“r”,“id”,”);你可以接受自己的正确答案。
        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;       
    }