C # Some XML operations

Source: Internet
Author: User
Tags xslt

1. XMLSCHEMA

There are two methods:

1. perform verification in the DOM Model

// Xmldocument Doc = new xmldocument ();

// Load syntax
// Doc. schemas. Add ("www.ljzforever.com", "XSD. XSD ");

// Load the document
// Doc. Load ("XML. xml ");

// Perform verification and specify the method triggered when an error occurs during verification
// Doc. Validate (validatehandler );

 

2. perform verification with the parameter object

Xmlreadersettings set = new xmlreadersettings ();

// Load syntax
Set. schemas. Add ("www.ljzforever.com", "XSD. XSD ");

// Verification type
Set. validationtype = validationtype. Schema;

// Specify the method triggered when an error occurs during verification
Set. validationeventhandler + = validatehandler;

// Load the parameter object into the reader
Xmlreader READ = xmlreader. Create ("XML. xml", set );

// Verify the file during the document loading process
While (read. Read ()){}

// The above sentence or use:

// Xmldocument Doc = new xmldocument ();
// Doc. Load (read );

 

/// Method triggered when an error occurs during verification

Static void validatehandler (Object sender, validationeventargs E)
{
Console. writeline (E. Message );
}

 

2. XSLT

// Load

Extends compiledtransform TRAN = new extends compiledtransform ();
Tran. Load ("XSL. XSL ");

// Prepare the stream
Memorystream MS = new memorystream ();
Xmltextwriter xtw = new xmltextwriter (MS, encoding. utf8 );

// Prepare Parameters
Inclutargumentlist list = new inclutargumentlist ();
List. addparam ("level", "", 3 );

// Transfer the result to the stream
Tran. Transform ("XML. xml", list, xtw );

// Transfer the stream to a string
Byte [] buffer = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (buffer, 0, (INT) ms. Length );
Console. writeline (system. Text. encoding. utf8.getstring (buffer ));
Console. readkey ();

 

The above is only the general situation. If you need to input the node as a parameter in XSLT, You need to introduce a new object.

// Use xpathdocument to load the document

Xpathdocument Path = new xpathdocument ("XML. xml ");

// Create a navigation
Xpathnavigator nav = path. createnavigator ();

// Select the node set in the navigation bar, which is the XPath syntax
Xpathnodeiterator iter = nav. Select ("// EMP ");

// Import the node set to the parameter value

List. addparam ("level", "", ITER );

 

3. About xmldatadocument object

Through xmldatadocument object, we can establish a relationship with dataset, but in my research, we found that dataset is the main class between these two objects, and xmldatadocument is mainly used in combination. (Haha, I personally agree), because dataset is required to load the schema at any time, otherwise it will not be available!

There are three ways to establish the relationship between the two.

A. first create a dataset, load the XML document, and then establish a relationship in the xmldatadocument constructor:

Dataset DS = new dataset ()

DS. readxml ("XML. xml ");

Xmldatadocument Doc = new xmldatadocument (DS)

B. first create a dataset, load the XML architecture, then establish a relationship in the xmldatadocument constructor, and finally load the document in xmldatadocument:

Dataset DS = new dataset ()

DS. readxmlschema ("XSD. XSD ");

Xmldatadocument Doc = new xmldatadocument (DS)

Doc. Load ("XML. xml ");

C. First create xmldatadocument, then create dataset, and assign the dataset attribute of xmldatadocument to it, then the dataset loads the architecture, and finally the xmldatadocument loads the document

Xmldatadocument Doc = new xmldatadocument (DS)

Dataset DS = data. dataset;
DS. readxmlschema ("Ds. XSD ");
Data. Load ("XML. xml ");

 

In short, no matter how it is created, dataset requires an architecture and cannot be later than document loading. It must be loaded at the same time or first!

By introducing xmldatadocument, the data in dataset can be operated both in ado.net mode and in xml dom model or stream model, enhancing the operation flexibility.

 

Note:

1. The third constructor is used for streaming, because it can avoid XML file distortion. IfDatasetYesReadxmlFilled in XML files, when you useWritexmlWhen data is written back as an XML document, the data may be significantly different from the initial XML document. This is becauseDatasetThe format settings (such as blank space) or hierarchical information (such as element order) in the XML document are not maintained ).DatasetDoes not contain XML documents because they do not match.DatasetElements ignored by the architecture. EnablingXmldatadocumentAndDatasetSynchronization, you canXmldatadocumentTo maintain the format settings and hierarchical element structure of the initial XML document.DatasetOnly applicableDatasetData and Architecture Information.

2. dataset requires an architecture and cannot be later than document loading. It must be loaded at the same time or first! As described above

3. All places where xmldocument is used can be usedXmldatadocument, because xmldatadocument isXmldocument Extension

 

 

ReferenceArticle:

Xmldatadocument and Dataset

Http://software.it168.com/manual/asp.net/4-4-6.htm

Example of synchronizing dataset with xmldatadocument (programming of nodes and fields)

Http://www.cnblogs.com/cuihongyu3503319/archive/2007/01/07/614072.html

Let's talk about the differences between the xmldatadocument class and the dataset class.

Http://topic.csdn.net/t/20040920/02/3389023.html

Differences between xmldocument and xmldatadocument

Http://www.cftea.com/c/2008/07/Q1DY3YTFNQ8V95LB.asp

Xmldatadocument Problems

Http://topic.csdn.net/t/20050603/07/4055616.html

Parsing XML programming technology under the. NET Framework

Http://blog.csdn.net/qdzx2008/archive/2006/04/17/666566.aspx

Ask xmldatadocument

Http://topic.csdn.net/t/20050420/14/3952062.html

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.