In .Net Framework, the System.Xml library, the easiest way to find a node by XmlNode.SelectSingleNode Xml XmlNode.SelectNodes and methods, XPath string parsing. However, if the text contains Xml namespace (Namespace), things are a little complicated.
Note, Xml namespace belonging to the paradox of technology, see Xml namespace (Namespace) Introduction and analysis of the pros and cons .
It contains Xml namespace (Namespace) prefix
For example, the following example, xdoc.SelectSingleNode not find anything, can only get Nothing.
Dim xml As String = "<test:store xmlns:test='testnamespace'><test:book/></test:store>"
Dim xdoc As XmlDocument = New XmlDocument
xdoc.LoadXml(xml)
Dim xnd As XmlNode = xdoc.SelectSingleNode("//book")
Correct code is as follows.
Dim xml As String = "<test:store xmlns:test='testnamespace'><test:book/></test:store>"
Dim xdoc As XmlDocument = New XmlDocument
xdoc.LoadXml(xml)
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xdoc.NameTable)
nsmgr.AddNamespace("test", "testnamespace")
Dim xnd As XmlNode = xdoc.SelectSingleNode("//test:book", nsmgr)
For XPath contains prefix must be used XmlNamespaceManager parameters, otherwise there will be a SelectSingleNode Exception.
Xml contains the default namespace (Namespace) of
If Xml namespace is the default, still have to use XmlNamespaceManager. Microsoft does not currently support the XPath default namespace, and this is known as specification defined Xml namespace. However, many other Xml analyzers such as XmlSpy support top-level node of the default namespace.
For example, the following example, xdoc.SelectSingleNode also can not find anything, can only get Nothing.
Dim xml As String = "<store xmlns='testnamespace'><book/></store>"
Dim xdoc As XmlDocument = New XmlDocument
xdoc.LoadXml(xml)
Dim xnd As XmlNode = xdoc.SelectSingleNode("//book")
If you want to find the book node, you need to use the following code.
Dim xml As String = "<store xmlns='testnamespace'><book/></store>"
Dim xdoc As XmlDocument = New XmlDocument
xdoc.LoadXml(xml)
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xdoc.NameTable)
nsmgr.AddNamespace("test", "testnamespace")
Dim xnd As XmlNode = xdoc.SelectSingleNode("//test:book", nsmgr)
This code is very strange, obviously XPath prefix may not be needed, but now they have to be prefixed, but this is Microsoft Well, what way, people in the .Net Framework library reference manual - XmlNode.SelectSingleNode Method , a special description, so have to write code for the job.
Ignore Xml namespace (Namespace) code
But also it allows to ignore XmlDocument Xml namespace (Namespace) when loaded. Sample code is as follows.
Dim xml As String = "<store xmlns='testnamespace'><book/></store>"
Dim srd As StringReader = New StringReader(xml)
Dim xrd As XmlTextReader = New XmlTextReader(srd)
xrd.Namespaces = False
Dim xdoc As XmlDocument = New XmlDocument
xdoc.Load(xrd)
Dim xnd As XmlNode = xdoc.SelectSingleNode("//book")
The main principle of this example is that there is a Namespaces property in the XmlTextReader, set in the future to False, it will not be as xmlns namespace (Namespace) processing, and use it as a general property. This approach is only a default text Xml namespace, can play a role simplifies XPath, but if the text is more Xml namespace mix, it will conflict Tag name and attribute name conflicts may occur.
Further, this method is that if the InnerXml to obtain, or XmlDocument output or stored, there will Exception, need only be done XmlTextWriter, Namespaces XmlTextWriter also have a property to be set to False, and so it will not output when saving, the xmlns attribute as a general treatment.
Bobo slope original article link: http://www.bobopo.com/article/code/dotnet_xml_namespace.htm
Keywords: .Net Framework, DotNet, the Xml, namespaces, Namespace, SelectSingleNode, the default namespace, Xmlns
Creation Date: 2008-01-22
dotNet Related Articles
- Xml data to contain illegal characters (Invalid Character) of the fault-tolerant (2008-03-04)
- Microsoft released Studio 2008 Hot-Fix Visual Roll-Up (2008-03-04)
- In using .Net OctreeQuantizer Gif files in improving picture quality (2008-02-26)
- Debugging tools and methods to call native (Native) Dll in managed (Managed) code (2008-01-20)
- Xhtml loaded with XmlDocument.LoadXml .Net file in time is very slow (2008-01-12)
- ...... more articles ......
IT Related Articles
- Trial experience and knowledge of the iTune v7.6.0.29 for the AAC audio format (2008-02-11)
- Install K-Lite Mega Codec v3.7.5 and Recognition for the MPC (2008-02-10)
- Driver Kodak USB Picture Card Reader 1.07 SP2 does not support XP (2008-01-29)
- Xml namespace (Namespace) Brief description and analysis of the pros and cons (2008-01-22)
- Hard detection, life, a hard disk, and detecting the SMART software (2008-01-13)
- ...... more articles ......
Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/04/09/2438418.html