This article describes the sample code for XmlDocument operations. it has some reference value. let's take a look at it.
XmlDocument example code for operating xml documents
xml test
sa
sa
xml document
Reads an attribute from a node.
XmlDocument doc=newXmlDocument(); doc.Load("config.xml"); XmlNodexnuser=doc.SelectSingleNode("userdata"); stringflag=xnuser.Attributes["createuser"].InnerText;
Read the value from the node
XmlDocument doc=newXmlDocument(); doc.Load("config.xml"); XmlNode xnserver =doc.SelectSingleNode("userdata/dataconnection/server");
Modify node attributes
XmlDocument doc=newXmlDocument(); doc.Load("config.xml"); XmlNodexnuser=doc.SelectSingleNode("userdata"); xnuser.Attributes["createuser"].InnerText="false"; doc.Save("config.xml");
Append node
XmlDocument doc = newXmlDocument (); XmlTextReader reader = newXmlTextReader ("config. xml "); doc. load ("config. xml "); XmlElement root = doc. documentElement; // get the root node XmlElement tagMessage = doc. createElement ("net"); XmlElement tagText = doc. createElement ("name"); tagText. innerText = netname; tagMessage. appendChild (tagText); // append to the root of the xml text. appendChild (tagMessage); reader. close (); // Close XmlTextReader doc. save ("config. xml "); // Save the xml file
The above is the details of the sample code used to operate xml documents in XmlDocument. For more information, see other related articles in the first PHP community!