1. Set-executionpolicy unrestricted
Powershell is very powerful and convenient, but it is often difficult to intelligently prompt. It can only be implemented in other languages and then converted to PSL. This was met at work, but I went home and thought about it and improved the code. It is only a small part of implementation, and there is no parameterization. It doesn't matter. Click.
[String] $ xmldocpath = "F:/testdata/testxml. XML "<br/> [String] $ nodename =" property "</P> <p> $ xmldoc = new-object" system. XML. xmldocument "<br/> $ xmldoc. load ($ xmldocpath) <br/> $ nodelist = $ xmldoc. getelementsbytagname ($ nodename); <br/> foreach ($ node in $ nodelist) <br/>{< br/> # use attributes will throw error. <br/> # $ namestr = $ node. attributes [0]. innerxml <br/> # Write-host "$ namestr" <br/> $ namestr = $ node. getattribute ("value") <br/> write-host "$ namestr" <br/> $ childnodes = $ node. childnodes; <br/> foreach ($ childnode in $ childnodes) <br/>{< br/> $ textstr = $ childnode. innerxml. tostring () <br/> write-host "$ textstr" <br/>}< br/>
C # code:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. XML; <br/> # region <br/> namespace xmloperation <br/> {<br/> class Program <br/>{< br/> static string xmlpath = @ "F: /testdata/testxml. XML "; </P> <p> static void main (string [] ARGs) <br/>{< br/> xmldocument xmldoc = new xmldocument (); <br/> xmldoc. load (xmlpath); </P> <p> xmlnodelist nodelist = xmldoc. getelementsbytagname ("property"); <br/> foreach (xmlnode node in nodelist) <br/>{< br/> xmlelement element = (xmlelement) node; <br/> string STR = node. value; <br/> string strname = node. attributes [0]. innerxml; <br/> strname = element. getattribute ("name"); <br/> console. writeline (strname); <br/> xmlnodelist childnodes = element. childnodes; <br/> foreach (xmlnode childnode in childnodes) <br/>{< br/> console. writeline (childnode. innerxml. tostring ()); <br/>}</P> <p> # endregion <br/>
XML file:
<? XML version = "1.0" encoding = "gb2312"?> <Br/> <root> <br/> <property name = "first" value = "value"> <br/> <guid> xxoxx </GUID> <br/> <Tel> 123456 </Tel> <br/> <Start> 5.95 </start> <br/> </property> <br/> <property name = "second"> <br/> <guid> xxoxx </GUID> <br/> <Tel> 123456 </Tel> <br/> <Start> 5.95 </start> <br/> </property> <br/> </root> <br/>