Processing Xml namespace (Namespace) in the .Net Framework

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.

Author:  Jay stick [Jegwon]

Bobo slope original article  link: http://www.bobopo.com/article/code/dotnet_xml_namespace.htm

Tags:  dotNetIT

Keywords:  .Net Framework, DotNet, the Xml, namespaces, Namespace, SelectSingleNode, the default namespace, Xmlns

Creation Date:  2008-01-22

 

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/04/09/2438418.html

Guess you like

Origin blog.csdn.net/weixin_33811961/article/details/93495106