Home > Backend Development > PHP Tutorial > When XmlDocument innovates objects, the solution to generate xmlns=''_PHP Tutorial

When XmlDocument innovates objects, the solution to generate xmlns=''_PHP Tutorial

WBOY
Release: 2016-07-20 11:02:56
Original
809 people have browsed it

When I encountered the XmlDocument innovative object during development today, xmlns="" was generated. After analysis, I summarized the problem. If you need a reference, it may help you solve this problem. ​


1. When reading, NameSpace.

should be added

The code is as follows Copy code
var exclDef = excl.SelectSingleNode(@ "ns:worksheet/ns:sheetData", excl.GetNameSpaceManager("ns")); 
 代码如下 复制代码
var exclDef = excl.SelectSingleNode(@"ns:worksheet/ns:sheetData", excl.GetNameSpaceManager("ns"));      
 public static XmlNamespaceManager GetNameSpaceManager(this XmlDocument xml, string NameSpace)       
{           
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);            nsmgr.AddNamespace(NameSpace, xml.DocumentElement.NamespaceURI);            return nsmgr;       
}
public static XmlNamespaceManager GetNameSpaceManager(this XmlDocument xml, string NameSpace)

{                                             XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);  }

2. If you create a node without specifying NamespaceURI, xmlns="" will be generated. For the reason, please refer to:

/post_read.asp?BlogID=2524806&PostID=21711977


If the three-level NameSpaceURIs of ABC are XYY, then the generated NameSpaceURIs of ABC are AB empty, and C will not generate NameSpaceURI, because C and its parent have the same NameSpaceURI.


So when creating a node, in order to avoid NameSpaceURI appearing, please keep the same NameSpaceURI as the node parent.


var si = e.OwnerDocument.CreateElement("si", e.NamespaceURI); e.AppendChild(si);

In addition, when innovating attributes, do not specify NameSpaceURI, that is, the generated Xml will not have a specific NameSpaceURI.

var r = excl.CreateAttribute("r");

http://www.bkjia.com/PHPjc/445332.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445332.htmlTechArticleWhen I encountered the XmlDocument innovative object during development today, xmlns= was generated. After analysis, I summarized the problem. If you need a friend's reference, it may be able to help you solve this problem. 1...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template