Load Xml document

Source: Internet
Author: User

After creating an Xml document, we can save the data we need to save to the Xml file as the Xml data format. When we need to use it in the future, we can access the data by loading the Xml document. So how to load an Xml data?

1. Load the Xml document in C.

Parsing Xml configuration files in config format is a good method, but it is not commonly used. Here I use a class of Xml documents to load an Xml file in the disk into the memory. This class is the XmlDocument class, which represents an Xml document.

The XmlDocment class provides a Load method to Load Xml documents. For detailed parameters and overloading, see the description in MSDN. Note that you need to add the System. Xml namespace to use this class.

The following example shows how to use some methods of this class.

First, create an application and add three buttons on the interface. Use the default name.

Then add a click event for the three buttons. And copy the following code to the Code view.

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;

Using System. Xml; // note the namespace to be introduced.

Namespace CS_Xml_Demo
{
Public partial class Form1: Form
{
XmlDocument xmlDoc = null;

Public Form1 ()
{
InitializeComponent ();
}

Private void button#click (object sender, EventArgs e)
{
XmlDoc = new XmlDocument (); // use the new keyword to create an Xml document.
MessageBox. Show ("complete creation ");
}

Private void button2_Click (object sender, EventArgs e)
{
If (xmlDoc = null)
{
MessageBox. Show ("You have not created any Xml document! ");
Return;
}
String path = @ "book. xml ";

Try
{
XmlDoc. Load (path );
MessageBox. Show ("finished loading ");
}
Catch (Exception ex)
{
MessageBox. Show ("Error", ex. Message );
}
}

Private void button3_Click (object sender, EventArgs e)
{
If (xmlDoc! = Null)
MessageBox. Show (xmlDoc. InnerXml );
Else
MessageBox. Show ("the Xml document has not been initialized! ");
}
}
}

 

2. Load the Xml document in Javascript.

When working on Ajax or RIA applications, friends often need to read Xml data files. When loading Xml documents using JavaScript, you must first create an Xml DOM object using JavaScript:

Var xmlDoc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");

After creating an Xml DOM object, you must specify a file path, for example, var path = "book. xml". Then, use the following code to load the xml:

XmlDoc. async = false;
XmlDoc. validateOnParse = true;
XmlDoc. load (path );

The above is only one method. Another method is to use the Xml data island to load Xml documents. Data island can also be directly bound to HTML without writing a large amount of code to complete the binding. For more information about Xml data island, see the next article.

 

Statement

Author: Qi FeiEmail: youring2@gmail.com
You are welcome to reprint this paragraph, but please keep this paragraph information!

 

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.