XmlDocument 创新对象时, 产生 xmlns="" 的解决方案
今天做开发时碰到的XmlDocument 创新对象时, 产生 xmlns="" 的,经过分析总结了问题所在,有需要朋友参考一下,可能能帮助你解决这个问题。
1. 读取时,应该添加NameSpace.
代码如下 | 复制代码 |
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; } |
2. 如果创建 节点,没有指明 NamespaceURI , 则会生成 xmlns="" , 其原因,参考:
/post_read.asp?BlogID=2524806&PostID=21711977
如果 ABC 三级的 NameSpaceURI 分别是 XYY , 那么, 生成的 ABC 的 NameSpaceURI分别是 AB空, C 不会生成 NameSpaceURI , 因为, C和其父项具有相同的 NameSpaceURI .
所以在创建 节点时, 为了不出现 NameSpaceURI , 请保持和节点父项相同的 NameSpaceURI .
var si = e.OwnerDocument.CreateElement("si", e.NamespaceURI); e.AppendChild(si);
另外, 创新 属性时, 不要指定 NameSpaceURI , 即 生成的 Xml 就没有特定的 NameSpaceURI .
var r = excl.CreateAttribute("r");

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)