Reading XML documents with code

Source: Internet
Author: User
Tags httpcontext

Currently, there are many pages that use the configuration of XML files to read, and the following explains how to use vs.net for automatic processing.

(the picture is wrong) 1 , using XML Generate XSD

Create a new space solution with Visual Studio.NET, as shown in the following illustration, because the test here is just to verify that the XML can be used to generate an XSD automatically, so you don't have to choose a asp.net Web application.

When the project is established, we add an XML document to the project by right-clicking the project in Solution Explorer and selecting Add/Add New item/image below

Select "XML file" in the template type to name the established XML document Testcfg.xml.



Enter the following XML text
? XML version= "1.0" standalone= "yes"?>
< Site >
< tab > Home </tab >
< module > Login </Module >
</Site >



The cursor focus must be within an XML document, and you can see the XML menu on the Visual Studio.NET menu, and choose Create Schema from the Drop-down menu as shown below



This vs. will establish testcfg.xsd based on the XML document. The code is as follows: XML version= "1.0"?>

< Xs:schema ID = "NewDataSet" targetnamespace = "http://tempuri.org/TestCfg1.xsd" Xmlns:mstns = "http://tempuri.org/ Testcfg1.xsd "

xmlns = "http://tempuri.org/TestCfg1.xsd" Xmlns:xs = "Http://www.w3.org/2001/XMLSchema" xmlns:msdata = urn: Schemas-microsoft-com:xml-msdata "

attributeFormDefault = "qualified" elementFormDefault = "qualified" >

< xs:element name = "Site" >

< Xs:complextype >

< xs:sequence >

< xs:element name = "tab" type = "xs:string" minoccurs = "0"/>

< xs:element name = "Module" type = "xs:string" minoccurs = "0"/>

</xs:sequence >

</Xs:complextype >

</xs:element >

< xs:element name = "NewDataSet" Msdata:isdataset = "true" Msdata:locale = "ZH-CN" msdata:enforceconstraints = "False" >

< Xs:complextype >

< Xs:choice maxOccurs = "unbounded" >

< xs:element ref = "Site"/>

</Xs:choice >

</Xs:complextype >

</xs:element >

</Xs:schema >


This completes the purpose of generating XSD from XML. But this build is generated automatically, so the generated XSD does not necessarily fully meet our requirements, which requires manual changes to some of the XSD code, where we simply test without changing the

2 , from XSD Generate CS class file

Generating a CS file from an XSD file requires the help of Microsoft's XSD.exe file, which is installed with VS, and the default is placed under the Sdk/v1.1/bin folder, using the format:

xsd file.xsd {/classes |/dataset} [/element:element]

[/language:language] [/namespace:namespace]

[/outputdir:directory] [Uri:uri]

Where the parameter/d[ataset] generates a class derived from the DataSet that corresponds to the specified schema.

Parameter/n[amespace]:namespace specifies the Run-time library namespace for the generated type. The default namespace is Schemas.

The/D and/n parameters are also used in the portal. Below we will use the/d parameter to transform the testcfg.xsd generated above into a TestCfg.cs class file.

If you have not set the file path you can use path to set the following figure

In the code above, by

xsd/d testcfg.xsd

The TestCfg.cs is generated and the file code is as follows:


// ------------------------------------------------------------------------------
<autogenerated>
This code is generated by a tool.
Runtime version:1.1.4322.573
//
Changes to this file may cause incorrect behavior and would be lost if
The code is regenerated.
</autogenerated>
// ------------------------------------------------------------------------------
//
This source code is generated automatically by XSD, version=1.1.4322.573.
//
Using System;
Using System.Data;
Using System.Xml;
Using System.Runtime.Serialization;
[Serializable ()]
[System.ComponentModel.DesignerCategoryAttribute ("code")]
[System.Diagnostics.DebuggerStepThrough ()]
[System.ComponentModel.ToolboxItem (True)]
public class Newdataset:dataset {
Note that this is the NewDataSet class.
The code is very long, test it yourself ^_^
//
Private Sitedatatable Tablesite;
Public NewDataSet () {

}

You can see more code, but composite C # syntax, so you can compile

3 , will TestCfg.cs compiled to TestCfg.dll file

Compiling TestCfg.cs as a TestCfg.dll file requires the automatic CSC.exe this file with the. NET framework, which is installed in the frameword/v1.1.4322 directory as shown below

This generates the TestCfg.dll file

Since we do not have a custom namespace, we can see later that the definition object is NewDataSet

4. Use TestCfg.dll file

After adding TestCfg.dll to any project, you can read/write XML documents, where the ReadXml and WriteXml methods are used for reading/writing. The sample code is as follows:

private void Page_Load (object sender, System.EventArgs e)

{
NewDataSet MySite = (newdataset) httpcontext.current.cache["MySite"];
if (MySite = null)

{
MySite = new NewDataSet ();
String configfile = HttpContext.Current.Server.MapPath ("Testcfg.xml");
MySite. READXML (ConfigFile);
MySite. Site.addsiterow ("Home", "News");
MySite. WriteXml (ConfigFile);
}

So you open the XML document

You will find that Testcfg.xml has added new data as follows: xml version= "1.0"  standalone= "yes"?>
< newdataset& NB

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.