Read and write XML

Source: Internet
Author: User

Xml:

Code:

//instantiation ofXmlDocument XMLDC =NewXmlDocument (); //loads the XML file, and the parameter is the path. Xmldc. Load ("C:/users/jin/desktop/1.xml"); //Gets the node list of the root element CompanyXmlNodeList nodelist = xmldc. selectSingleNode (" Company").            ChildNodes; //1, query the known absolute path node (set)//Result: 3 employee nodes were obtainedXmlNodeList objNodeList = xmldc. SelectNodes ("Company/department/employee"); //2. Return to the first node//results: In 3 nodes, only the first one is takenXmlNode objnode = xmldc. selectSingleNode ("/company/department/employee"); //3. Query the node (set) of the known relative path//Result: Data under the first Department nodeObjnode = Xmldc. selectSingleNode ("company/department"); //Result: objNodeList not getting data (wrong)objNodeList = Xmldc. SelectNodes (".. /department"); //4. Querying nodes (sets) for known element names//when using irregular hierarchical documents, because you do not know the element names at the middle level, you can use the//symbol to cross the middle of the node, querying its child, grandchild, or all other elements under multiple levels. //Result: 3 employee nodes were obtainedobjNodeList = Xmldc. SelectNodes ("Company//employee"); //5. Query Properties (attribute) node//Each of the above methods returns the element node (set), the return attribute (attribute), only need to use the corresponding method, before the property name with an @ symbol can be//Result: The property of the returned employee has 2 data with IDobjNodeList = Xmldc. SelectNodes ("company/department/employee/@Id"); //Result: returned attribute with ID of 3 data (refers to: Employee node of 2, department_name1 bar)objNodeList = Xmldc. SelectNodes ("company//@Id"); //6. Query text node using text () to get the text node//Result: Financial department data returnedObjnode = Xmldc. selectSingleNode ("Company/department/department_name/text ()"); //7. Nodes that query specific criteria use the [] symbol to query for nodes of a particular condition. //Result: Returns the John Doe dataObjnode = Xmldc. selectSingleNode ("company/department/employee[@Id = ' 1 ']"); //result: Returns the Harry dataObjnode = Xmldc. selectSingleNode ("//employee[@Id = ' 2 ']"); //Result: Returns the John Doe dataObjnode = Xmldc. selectSingleNode ("company/department/employee/name[text () = ' John Doe ']"); //Result: Return to finance Department dataObjnode = Xmldc. selectSingleNode ("company/department[employee/@Id = ' 2 ']/department_name"); //8. Querying multiple-mode nodes//Use the | symbol to get multiple-mode nodes. //Results: 4 articles were obtained, respectively, department_name2, Manager2 strips. objNodeList = Xmldc. SelectNodes ("Company/department/department_name | Company/department/manager"); //9. Query any child nodes use the * symbol to return all child nodes of the current node. //result: Gets the Manager2 bar. objNodeList = Xmldc. SelectNodes ("Company/*/manager"); //result: In the returned innertext, there is all the XML contentobjNodeList =XMLDC.            ChildNodes; //Editing of XML data//add an element's attribute (attribute) node//The result: Add the value to the Finance department. XmlAttribute objnodeattr = xmldc. CreateAttribute ("ID"); Objnodeattr.innertext="101";            ObjNode.Attributes.Append (OBJNODEATTR); //Delete an element's propertiesObjNode.Attributes.Remove (objnodeattr); //Add a child element (an element) (wrong, change later)//objnodechild = xmldc. CreateElement (Nothing, "ID", nothing);

There are a lot of not try to succeed in the future to get.

Read and write XML

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.