1. The difference between elements and attributes: You can add child elements or attributes to an element, but not to attributes, and to transfer documents on the network without compression, the feature consumes less bandwidth.
2. The Declaration of XML in VS is only supported in version 1.0: <?xml Version = "1.0"?>
3. Namespace: Xmlns:namespace1 = ""
4. Validate XML document Common patterns rather than document type definitions (DTDs). The common pattern format is XSD rather than XDR.
Note: Elements in the XSD schema must belong to the namespace: Http://www.w3.org/2001/XMLSchema; otherwise, these schema elements will not be recognized.
5, with the element is using the XmlDocument class properties documentelement obtained.
6. Recursive display of XML document content:
Private stringFormattext (XmlNode node,stringTextstringindent) { if(node isXmlText) {Text+=node. Value; returntext; } if(string. IsNullOrEmpty (indent)) Indent=""; Else{text+="\ r \ n"+indent; } if(node isxmlcomment) {Text+=node. OuterXml; returntext; } text+="<"+node. Name; if(node. Attributes.count >0) {addattributes (node,reftext); } if(node. HasChildNodes) {text+=">"; foreach(XmlNode Childinchnode. ChildNodes) {text= Formattext (Child, text, indent +" "); } if(node. Childnodes.count = =1&&(node. FirstChild isXmlText | | Node. FirstChild isxmlcomment)) Text+="</"+ node. Name +">"; Elsetext+="\ r \ n"+ Indent +"</"+ node. Name +">"; } Elsetext+="/>"; returntext; } Private voidAddAttributes (XmlNode node,ref stringtext) { foreach(XmlAttribute xainchnode. Attributes) {text+=" "+ XA. Name +"= '"+ XA. Value +"'"; } }
View Code
Note: A node's child nodes may not be XmlNode, or xmltext or xmlcomment;createtextnode can create xmltext.
7. The XmlNode class contains two methods (selectSingleNode and selectnodes) that are often used to select nodes from a document and do not traverse each of them, using a special query language XPath to select nodes.
C#xml operation