This evening, I saw a question in the blog: Find the node with specific attribute values in the XML file and add a new attribute to it. I thought it was very simple and I wrote one with my handwriting.ProgramBut I encountered a problem, mainly because of multiple namespaces. I found some materials and finally solved the problem. Now I want to share the solution, if you have a better solution, please leave a message for discussion.
The XML file is as follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Enginuity: viewcontrol
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"
Xmlns: enginuity = "CLR-namespace: enginuity. Core; Assembly = enginuity. Core" >
< Viewbox Stretch = "Fill" >
< Textbox Name = "Text_10" Value = "ABC" />
< Textbox Name = "Text_11" Value = "BCD" />
</ Viewbox >
</ Enginuity: viewcontrol >
Here is a small note: enginuity: viewcontrol indicates that the namespace of viewcontrol is enginuity.
The Value Attribute Value of the textbox node whose name is text_10 is ABC.
About selectsinglenode method you can refer to: http://msdn.microsoft.com/en-us/library/h0hw012b.aspx
This method has two parameters. The first one is string XPath., This is required, and the second isXmlnamespacemanager nsmgr, which is optional.The most important thing is the writing of XPath, mainly the namespace: node/.... In the official example, there is only one namespace, and the XML structure is relatively simple.
From the XML file above, it is easy to see that the namespace of viewcontrol is enginuity, but what is the namespace of viewbox? After searching for the definition of the namespace of the XML file, we found this sentence"If the namespace of the current node is not explicitly specified in the XML document, the namespace of the current node inherits the namespace of its parent node."The parent node of viewbox is viewcontrol, And the namespace of viewcontrol is enginuity. It is also noted that enginuity is not the final namespace, And the namespace of enginuity is xmlns. Which one is it?
Try it out and write the following program:
Xmldocument dom = New Xmldocument ();
Dom. Load ( @" E: \ net \ test. xml " );
Xmlnamespacemanager xnm = New Xmlnamespacemanager (DOM. nametable );
Xnm. addnamespace ( " E " , " Http://schemas.microsoft.com/winfx/2006/xaml/presentation " );
Xnm. addnamespace ( " X " , " Http://schemas.microsoft.com/winfx/2006/xaml " );
Xnm. addnamespace ( " D " , " Http://schemas.microsoft.com/expression/blend/2008 " );
Xnm. addnamespace ( " MC " , " Http://schemas.openxmlformats.org/markup-compatibility/2006 " );
Xnm. addnamespace ( " Enginuity " , " CLR-namespace: enginuity. Core; Assembly = enginuity. Core " );
Xmlnodelist xnodes = Dom. selectsinglenode ( " Enginuity: viewcontrol " , Xnm). childnodes;
Console. writeline (xnodes [ 0 ]. Namespaceuri );
The result is as follows:
As you can see, the namespace of viewcontrol is the top xmlns, and the following program is written:
Xmldocument dom = New Xmldocument ();
Dom. Load ( @" E: \ net \ test. xml " );
Xmlnamespacemanager xnm = New Xmlnamespacemanager (DOM. nametable );
Xnm. addnamespace ( " E " , " Http://schemas.microsoft.com/winfx/2006/xaml/presentation " );
Xnm. addnamespace ( " X " , " Http://schemas.microsoft.com/winfx/2006/xaml " );
Xnm. addnamespace ( " D " , " Http://schemas.microsoft.com/expression/blend/2008 " );
Xnm. addnamespace ( " MC " , " Http://schemas.openxmlformats.org/markup-compatibility/2006 " );
Xnm. addnamespace ( " Enginuity " , " CLR-namespace: enginuity. Core; Assembly = enginuity. Core " );
Xmlnode xnode = Dom. selectsinglenode ( " Enginuity: viewcontrol/E: viewbox/E: textbox [@ name = 'text _ 10'] " , Xnm );
Console. writeline (xnode. attributes [ " Value " ]. Value );
The effect after running is as follows:
"ABC" is coming soon.
Therefore, it is concluded that when the selectsinglenode method is used to parse XML files containing multiple namespaces, The namespace node is not explicitly marked, and its namespace is the namespace of the root node. If you are not sure, you can start from the root node and output the node namespace layer by layer.
Keyword: selectsinglenode, C # parses XML files, selectsinglenode multi-namespace,
Author: tianxingjian, self-improvement
Source: http://www.cnblogs.com/durongjian
This is the first blog site in this article. The copyright is shared by the author and the blog site.
The reprinted statement must be retained and the link to this Article should be provided in a prominent position on the page. Otherwise, the legal liability will be retained.