Simple application of WebService add delete

Source: Internet
Author: User

WebService is a remote invocation technology across programming languages and cross-operating system platforms, simply to store data in a project's folder

. NET based on the DOM core class

XmlDocument represents an XML document

XmlNode represents a single node in an XML document

XmlNodeList represents a Sort node collection

XmlElement represents an element

Xmlattribyte represents a property

Xmlattribytecollection represents a collection of attributes for an element node

XmlText represents the literal class tolerance of an element or attribute

--------------------------------------------------------------------------------------------------------------- ----------------------

Write the following add delete query code in the controller

[HttpPost] Public voidIndex (formcollection F) {stringXmlpath = Server.MapPath ("~/a/") +"_class.xml";            XmlDocument Doc; Try             {                stringId = f["Id"]; stringName = f["Name"]; stringAge = f[" Age"]; stringSex = f["Sex"]; stringphone=f["Phone"]; if(!System.IO.File.Exists (Xmlpath)) {                    //creating an XML Document ObjectDoc =NewXmlDocument (); //Create an XML document DescriptionXmlDeclaration del = doc. Createxmldeclaration ("1.0","Utf-8",NULL); //Creating the root nodeXmlNode root = Doc. CreateNode (XmlNodeType.Element,"Class",NULL); //Add a description to the documentDoc.                    AppendChild (DEL); //add a root node to a documentDoc.                    AppendChild (root); Doc.                Save (Xmlpath); }                //creating an XML Document ObjectDoc =NewXmlDocument (); //reads the specified XML fileDoc.                Load (Xmlpath); //reading the root node in XMLXmlElement element =Doc.                DocumentElement; //Create child nodesXmlElement Stu = doc. CreateElement ("Stu"); //set XML literal valuesStu.innertext =Name; //Set XML node PropertiesStu.setattribute ("Id", Id); Stu.setattribute ("Sex", Sex); Stu.setattribute (" Age", age); Stu.setattribute ("Phone", Phone); //to add a child node to the root nodeelement.                AppendChild (STU); //Save the XML document to the specified pathDoc.                Save (Xmlpath); Response.Write ("<script>alert (' Add success! '); window.location.href = '/home/index ';</script>"); }            Catch(Exception ex) {Response.Write ("<script>alert (' Add failed! '); window.location.href = '/home/index ';</script>"); }                    }         PublicActionResult Selanddel (stringID ="0")        {            if(id! ="0")            {                //to delete an operation                stringXmlpath = Server.MapPath ("~/a/") +"_class.xml"; XmlDocument Doc=NewXmlDocument (); Doc.                Load (Xmlpath); XmlElement element=Doc.                DocumentElement; XmlNode node= Doc. selectSingleNode (string. Format ("/class/stu[@id = ' {0} ']", id)); Node.                Parentnode.removechild (node); Doc.            Save (Xmlpath); }            Else            {                stringXmlpath = Server.MapPath ("~/a/") +"_class.xml"; StringBuilder SB=NewStringBuilder (); XmlDocument Doc=NewXmlDocument (); Doc.                Load (Xmlpath); XmlElement element=Doc.                DocumentElement; //Traverse all child nodes under the root node                foreach(XmlElement Iteminchelement. ChildNodes) {sb. Append (string. Format (@"<tr class= ' odd gradex ' > <td>{0}</td> <td>{1}</td> <td class= ' hidden-480 ' >{2                                            }</td> <td class= ' hidden-480 ' >{3}</td> <TD class= ' center hidden-480 ' >{4}</td> <td& Gt;<a href= '/home/selanddel/{5} ' > Delete </td> </tr>", item. attributes["Id"]. Value, item. Innertext,item. attributes[" Age"]. Value, item. attributes["Sex"]. Value, item. attributes["Phone"]. Value, item. attributes["Id"].                Value)); } Viewbag.tabletext=sb.            ToString (); }            returnView (); }

Write on the view page where the content is displayed

@Html. Raw (ViewBag.tableText.ToString ())//Converts HTML code strings written in the controller into HTML code to display execution in the foreground

Once added, the document will be generated under the folder you specified to save the value you entered.

<?xml version="1.0"encoding="Utf-8"?><class> <stu id="1"sex="female"Age="1"Phone="1">1</Stu> <stu id="0"sex="female"Age="0"Phone="0">0</Stu></Class>

Simple application of WebService add delete

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.