C # Read and Write XML files)

Source: Internet
Author: User
Methods for reading XML documents:

Using system;
Using system. xml;

Namespace readxml
{
Class class1
{
Static void main (string [] ARGs)
{
// Create an xmltextreader Class Object and call the read method to read the object
Xmltextreader textreader = new xmltextreader ("C: \ books. xml ");
Textreader. Read ();
// Execute the loop body if the node is not empty
While (textreader. Read ())
{
// Read the first element
Textreader. movetoelement ();
Console. writeline ("xmltextreader Properties Test ");
Console. writeline ("========================= ");

// Read the attributes of the element and display it on the console
Console. writeline ("name:" + textreader. Name );
Console. writeline ("base URI:" + textreader. baseuri );
Console. writeline ("local name:" + textreader. localname );
Console. writeline ("attribute count:" + textreader. attributecount. tostring ());
Console. writeline ("depth:" + textreader. Depth. tostring ());
Console. writeline ("line number:" + textreader. linenumber. tostring ());
Console. writeline ("Node Type:" + textreader. nodetype. tostring ());
Console. writeline ("attribute count:" + textreader. value. tostring ());
}
}
}
}
 


The xmltextreader class has a very important attribute-nodetype. Through this attribute, we can know the node type of the node. The enumerated type xmlnodetype contains XML items such as attribute, CDATA, element, comment, document, documenttype, entity, processinstruction, and whitespace. By comparing with the element in xmlnodetype, we can obtain the node type of the corresponding node and perform related operations on it. Next I will provide an instance that reads the nodetype of each node and displays its content based on the node type. The program also records the number of each node type in the XML file.

Using system;
Using system. xml;

Namespace readxml
{
Class class2
{
Static void main (string [] ARGs)
{
Int Ws = 0;
Int Pi = 0;
Int Dc = 0;
Int cc = 0;
Int AC = 0;
Int ET = 0;
Int El = 0;
Int XD = 0;

Xmltextreader textreader = new xmltextreader ("C: \ books. xml ");

While (textreader. Read ())
{
Xmlnodetype ntype = textreader. nodetype;

// The node type is xmldeclaration.
If (ntype = xmlnodetype. xmldeclaration)
{
Console. writeline ("Declaration:" + textreader. Name. tostring ());
XD = XD + 1;
}

// The node type is comment.
If (ntype = xmlnodetype. Comment)
{
Console. writeline ("comment:" + textreader. Name. tostring ());
Cc = CC + 1;
}

// The node type is attribute.
If (ntype = xmlnodetype. Attribute)
{
Console. writeline ("attribute:" + textreader. Name. tostring ());
AC = AC + 1;
}

// The node type is element.
If (ntype = xmlnodetype. element)
{
Console. writeline ("element:" + textreader. Name. tostring ());
El = EL + 1;
}

// The node type is entity.
If (ntype = xmlnodetype. entity)
{
Console. writeline ("entity:" + textreader. Name. tostring ());
ET = ET + 1;
}

// The node type is process instruction.
If (ntype = xmlnodetype. processinginstruction)
{
Console. writeline ("process instruction:" + textreader. Name. tostring ());
Pi = PI + 1;
}

// The node type is documenttype.
If (ntype = xmlnodetype. documenttype)
{
Console. writeline ("documenttype:" + textreader. Name. tostring ());
Dc = DC + 1;
}

// The node type is whitespace.
If (ntype = xmlnodetype. whitespace)
{
Console. writeline ("whitespace:" + textreader. Name. tostring ());
Ws = ws + 1;
}
}

// Display the number of each type in the console
Console. writeline ("Total comments:" + CC. tostring ());
Console. writeline ("Total attributes:" + AC. tostring ());
Console. writeline ("total elements:" + El. tostring ());
Console. writeline ("total entity:" + ET. tostring ());
Console. writeline ("total process instructions:" + pi. tostring ());
Console. writeline ("total declaration:" + XD. tostring ());
Console. writeline ("Total documenttype:" + DC. tostring ());
Console. writeline ("Total whitespaces:" + WS. tostring ());
}
}
}
 


The above section describes how to use the xmltextreader class object to read the XML document and obtain the node type information based on the node's nodetype attribute. At the same time, xmlreader is a base class, including xmlnodereader and xmlvalidatingreader. They are used to read the nodes and modes of XML documents. This article will not be introduced here. Readers can refer to the relevant materials.

Method for writing XML documents:

Using system;
Using system. xml;

Namespace writexml
{
Class class1
{
Static void main (string [] ARGs)
{
Try
{
// Create an Instance Object of the xmltextwriter class
Xmltextwriter textwriter = new xmltextwriter ("C :\\ w3sky. xml", null );
Textwriter. Formatting = formatting. indented;

// Start the write process and call the writestartdocument Method
Textwriter. writestartdocument ();

// Write description
Textwriter. writecomment ("first comment xmltextwriter sample example ");
Textwriter. writecomment ("w3sky. XML in root dir ");

// Create a node
Textwriter. writestartelement ("Administrator ");
Textwriter. writeelementstring ("name", "formble ");
Textwriter. writeelementstring ("Site", "w3sky.com ");
Textwriter. writeendelement ();


// When the document writing ends, call the writeenddocument method.
Textwriter. writeenddocument ();

// Disable textwriter
Textwriter. Close ();

}
Catch (system. Exception E)
{
Console. writeline (E. tostring ());
}
}
}
}


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.