Ajax example: XML format processing in ASP. NET

Source: Internet
Author: User
Document directory
  • XML document generated by the program
  • Perform operations on existing XML documents

In Ajax response data processing, the XML format is to return the XML document as the data content to the client for processing. The question is, how does the server program generate XML document files for data and how does the XML document operate the content?

XML document generated by the program

In this way, the C # program is used to generate an XML file and put the data in it for processing. The following are several different methods to achieve the same purpose.

A simple example of how to use the xmlwriter class to edit data:

XmlTextWriter xtw = new XmlTextWriter(filePath, Encoding.UTF8);xtw.Formatting = Formatting.Indented;xtw.WriteStartDocument(true);xtw.WriteStartElement("root");xtw.WriteElementString("username", Nodevalue);xtw.WriteEndElement();xtw.WriteEndDocument();xtw.Flush();xtw.Close();

In the sample code, nodevalue is the obtained data value, and the code is omitted. Filepath is the path value of the XML file. The code is omitted.

The running result generates an XML document with the same content as the previous post.

Example implemented using the xmldocument class:

XmlDocument xmldoc;XmlDeclaration xmldecl;XmlNode xmlnode;XmlElement xmlelem;xmldoc = new XmlDocument();xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);xmldoc.AppendChild(xmldecl);xmlelem = xmldoc.CreateElement("", "root", "");xmldoc.AppendChild(xmlelem);xmlnode = xmldoc.CreateElement("username");xmlnode.InnerText = Nodevalue;xmlelem.AppendChild(xmlnode);xmldoc.Save(filePath);

Note: The implementation of the xmldocument class is less efficient than that of the xmlwriter class. We recommend that you use the xmlwriter class.

Implemented Using XML serialization technology:

Root root = new Root();root.Username = Nodevalue;XmlSerializer srl = new XmlSerializer(typeof(Root));StreamWriter wrt = new StreamWriter(filePath);srl.Serialize(wrt, root);wrt.Close();

The following is the complete content of the root class:

using System;public class Root{    private string _username;    public string Username    {        get        {            return _username;        }        set        {            _username = value;        }    }}
Perform operations on existing XML documents

What if I want to operate on an existing XML document file instead of generating a new XML document file using a program? The following example is part of the C # programming code:

XmlDocument xmldoc = new XmlDocument();xmldoc.Load(filePath);XmlNodeList xmlnodelist = xmldoc.SelectSingleNode("Root").ChildNodes;foreach (XmlNode xmlnode in xmlnodelist){    while (xmlnode.Name == "Username")    {        xmlnode.InnerText = Nodevalue;        break;    }}xmldoc.Save(filePath);

The code is used to process existing XML files. You can see the following program example in the class constructor method:

if (File.Exists(filePath)){    XmlOperateHandle(); }else{    XmlWriterHandle();}

To improve the program performance, we use the xpathnavigator object processing to modify and rewrite the Code:

XmlDocument xmldoc = new XmlDocument();try { xmldoc.Load(filePath); }catch { }XPathNavigator navigator = xmldoc.CreateNavigator();navigator.MoveToRoot();if(navigator.HasChildren){    navigator.MoveToFirstChild();    if (navigator.Name == "username")    {        navigator.InnerXml = Nodevalue;    }}

Ajax blog articles:

Ajax: getting started with xhr

Ajax instance: XML Processing on ASP. NET Server

Ajax: use Asynchronous callback to refactor ASP. NET development instances

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.