PowerShell 20 Questions to play with the XML
PowerShell Missionary original article 2014-01-30,2015-10-16 to be reproduced, but must retain the name and provenance, otherwise investigate legal responsibility
Q: What is the encoding of XML files?
A: In order to better support the multi-character, the XML file is UTF8 or Unicode format, can not be processed with bat.
Q: What is the most basic format of an XML file?
For:
<?xml version= "1.0" encoding= "UTF-16"?>
< root node >
< root node/>
As mentioned above, there must be, and only one root node. and save the file in Unicode format.
Of course utf-8 is OK.
Q: What sub-projects are included in the XML file?
For:
Element (directory) ======element
Property =========attrib
Text =========text
Node =========xmlnode
Comments
cdata======== do not need to escape the content
"Processing instruction" ====== Save command name and command value
Q: PowerShell and. NET how to deal with XML, what is the advantage?
A: The traditional method of processing XML is to take XML as a plain text document to handle, that is, to use the regular to find the replacement.
While XML documents are structured, XML documents this directory structure. NET is called XPath. PowerShell and. NET uses XPath to quickly locate and read and write content in an XML file.
This is the most fundamental difference between them.
。 NET processing XML has several classes:
System.xml.XmlDocument, Https://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument (v=vs.110). aspx
SYSTEM.WEB.UI.WEBCONTROLS.XML, Https://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.xml (v=vs.110) . aspx
ASP. NET also seems to have XML classes, specifically forget.
Interested can study the similarities and differences of the points.
Q: PowerShell and. NET how to deal with XML, what is the advantage? ---------vs. vbs
For:
PowerShell and VBS deal with XML in a way that is similar to the syntax, and to some extent can be used to learn from each other.
But. NET itself has a good tutorial site, MSDN, can search the Chinese class and methods, properties of the tutorial.
。 NET class, which is more powerful than the XMLDOM class of VBS, has asynchronous read-write, LINQ to XML, supports T-SQL operation XML, of course, is not used in many cases.
PowerShell has an XML processing command select-xml, which is relatively straightforward to select nodes.
"Comparison of XPath and LINQ to XML" Https://msdn.microsoft.com/zh-cn/library/bb675156.aspx
"Manipulate XML with T-SQL" https://msdn.microsoft.com/zh-cn/library/aa175790 (v=sql.80). aspx
Q: There are several ways to read and write XML in. Net.
Answer: Two kinds of 1 xmlreader,xmlwriter. 2 using Dom method. The DOM method is mainly discussed here.
Q: How do i open an XML file?
For:
$ input File name = "A:\pscode\test74.xml"
[system.xml.xmldocument]$ Open XML file = get-content $ input file name
Q: How do I save an XML file?
For:
$ output File name = "A:\pscode\test74.xml"
$ Open XML file. Save ($ output file name)
Q: How does the XML file encode after I open the Save?
Answer: Same as source file. That is, the source file is Utf16, and is also UTF16 after storage. The original is UTF8, and after the UTF8 is also
Q: How do I select a node?
For:
The IF ($ Open XML file. selectSingleNode ("Node name 1/node Name 2")-ne $null)
{
The selected node = $ Open XML file. selectSingleNode ("Node name 1/node Name 2")
}
"Node name 1/section name 2[1]"--------Returns the value of the first element in 2
"Node name 1/section name 2[element <3]"--------returns all values of element less than 3 in nodes 2
"Node name 1/section name 2[last ()]"--------returns the last element value in 2
"Node name 1/section name 2[*]"--------returns all element values
"Node name 1//2"--------returns the content of Node 1, at any level, containing the nodes 2
"Node Name 1 | Node Name 2 "--------Returns a collection of nodes 1, node 2
"Node name 1[@abc]"--------Returns a collection of all the @abc properties in 1
The XML document operation, to a large extent, is the selection of nodes, the choice of this node, using the XPath syntax. Note XPath differs case
More XPath node selection syntax reference on the MSDN website.
https://msdn.microsoft.com/zh-cn/library/ms256471 (v=vs.110). aspx
Q: How do I select a node?
For:
PowerShell has an XML processing command select-xml, which is relatively straightforward to select nodes.
$ value 1 = (Select-xml-path $ Enter the file name-xpath "/AAA/BBB[1]/CCC"). node. Last nodes
$ value 2 = (Select-xml-path $ Enter the file name-xpath "/AAA/BBBB"). node. Last nodes
Https://msdn.microsoft.com/zh-cn/library/system.xml.xmlelement (v=vs.110). aspx
elements = = = Directory ===element common methods and properties:
Property
HasChildNodes gets a value that indicates whether this node has any child nodes.
ChildNodes gets all the child nodes of the node.
InnerText Gets or sets the concatenated value of the node and all its children
InnerXml Gets or sets a token that represents only this node's children.
IsEmpty Gets or sets the tag format of the element.
OuterXml Gets the markup that contains this node and all its child nodes.
Method
GetAttribute (String) Returns the value of the attribute with the specified name.
GetEnumerator Gets the enumeration that iterates over the child nodes of the current node
Hasattribute (String) determines whether the current node has an attribute with the specified name.
RemoveAttribute (String) removes an attribute by name.
RemoveChild removes the specified child node.
SetAttribute (String, string) sets the value of the attribute with the specified name.
Q: Please give an example of an XML file.
For:
-------------------------------------
<?xml version= "1.0" encoding= "Utf-8"?>
< root >
< three big Sun freaks >
< li Xiaolu >
<!--comment is a comment--><! [Cdata[in CDATA sections, all characters, including special characters, do not need to be escaped, so it is convenient to view and modify XML documents. ]]>< Is this lee a command-name command value? ></li Xiaolu >
< Vivian Hsu >text value = Son Lee v Bao </Vivian Hsu >
< Fanfan property value-birthplace = "Ohio, USA" ></Fanfan qi > Three big Baby Demon in the text value = Baby is the child's photos and other stickers on Weibo, brush, etc. to show off.
</three big Sun >
</root >
-------------------------------------
Q: How do I add, delete, and change comments?
For:
------------------------------------------
$ input File name = ' a:\pscode\l example. xml '
$ output File name = ' a:\pscode\l example 2.xml '
[system.xml.xmldocument]$ Open XML file = get-content $ input file name
The IF ($ Open XML file. selectSingleNode ("/root/three big-ne") ($null)
{
The selected node = $ Open XML file. selectSingleNode ("/root/three big Sun Demon")
}
[string]$ comment content to add = ' Who is the fourth one? ‘
$null = $ To add the Comment object = $ Open XML file. Createcomment ($ to add comment content)
$null = the selected node. AppendChild ($ to add annotation object)
#移除指定的子节点, with RemoveChild (object)
#编辑对象用, ReplaceChild (new, old)
$ Open XML file. Save ($ output file name)
-------------------------------------------
Q: How to increase, delete, change "CDATA"?
For:
Principle Ibid.
$null = $CDATA = $ Open XML file. Createcdatasection (CDATA name)
Q: How to increase, delete, change "processing instruction"?
For:
$null = $PI = $ Open XML file. CreateAttribute ("PI name", "PI value")
Q: How do I add, delete, and change elements? (node is directory)
For:
$null = $ To add the Node object = $ Open XML file. CreateElement ("Node name")
Q: How do I add, delete, and change attributes and values?
For:
$null = $ To add the Property Object = $ Open XML file. CreateAttribute ("Property name")
Q: How do I read element values?
Answer: 2 methods, similar.
1
The IF ($ Open XML file. selectSingleNode ("node name")-ne $null)
{
The selected node = $ Open XML file. selectSingleNode ("Node name")
}
$ element value = Selected node. Element name
2
The IF ($ Open XML file. selectSingleNode ("node name")-ne $null)
{
The $ element value = $ Open XML file. selectSingleNode ("node name"). Element name
}
Q: How to read plain text within a node
For:
$ node 1. InnerText
Q: How to read XML text within a node
For:
$ node 1. InnerXml
Q: How do I write element values?
For:
The IF ($ Open XML file. selectSingleNode ("node name")-ne $null)
{
$ Open XML file. selectSingleNode ("section name"). Element name = $ element value
}
PowerShell missionaries original articles allow reprint, but must retain the name and provenance, otherwise investigate legal responsibility
Q: How do I organize the indentation of an XML file?
Q: I have a large XML file, all things in a row, there are hundreds of trillion. How to add a reasonable change of line pinch? ---Hundreds of trillion? I've seen the biggest 10m!.
A: With PowerShell third-party module pscx,http://pscx.codeplex.com/, in the Format-xml command.
A: Open, edit, and then save, automatically organized.
----------------------------Reference------------------------------------
https://msdn.microsoft.com/zh-cn/library/ms256177 (v=vs.110). aspx
HTTPS://MSDN.MICROSOFT.COM/ZH-CN/LIBRARY/SYSTEM.XML (v=vs.110). aspx
PowerShell 20 Questions to play with the XML