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