C#xml operation

Source: Internet
Author: User
Tags validate xml document

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.