Read and write XML files in general format

Source: Internet
Author: User
Tags add format end tostring access
Xml

It is good to put the data into the database at once, but imagine if the user running the system does not have access to the network, that is, can not establish a connection with the database, then how to do? You can't just let it go without losing the data. Then the local data file becomes particularly important, and they become the media that hosts the data. However, the choice of file format for storing data requires careful consideration, and in the process of storing and reading data files, different file formats can have different effects on the performance of the program.

XML (Extensible Markup Language) file as the most closely related to the database file format is increasingly favored by programmers, let us look at its advantages:

"1" when used to describe a database, XML has two advantages over proprietary formats, such as access. mdb or dbase. dbf format: XML is human readable and is built on the basis of well-known, open standards.

The "2" XML describes the data itself. Because the data display is separate from the content, XML-defined data allows you to specify different display modes to make the data more reasonable. Local data can be dynamically expressed in the manner of client configuration, user selection, or other standard decisions.

"3" XML has a good value for preservation. The preservation of XML comes from one of its pioneers,--SGML language. SGML is a set of more than 10-year history of international standards, it initially designed a major goal is to provide documents for more than 50 years of life.

And it's not difficult to import data from an XML file into a database, so choosing an XML file as the bearer of the data is a great choice.

(read instance of XML)

XML follows strict syntax requirements, such as an XML file format as follows:

<ge name= "voltage meter" > entity name

<gee> the beginning of a figure data

<flagOrder>DrawLine</flagOrder>

<X>67</X>

<Y>23</Y>

<X1>37</X1>

<Y1>77</Y1>

<lw>1</lw>

<R>0</R>

<G>0</G>

<B>0</B>

</gee> the end of a graph element data

<gee>

<flagOrder>DrawRectangle</flagOrder>

<X>85</X>

<Y>76</Y>

<X1>30</X1>

<Y1>36</Y1>

<lw>1</lw>

<R>0</R>

<G>0</G>

<B>0</B>

</gee>

<gee>

<flagOrder>DrawEllipse</flagOrder>

<X>30</X>

<Y>7</Y>

<X1>70</X1>

<Y1>46</Y1>

<lw>1</lw>

<R>0</R>

<G>0</G>

<B>0</B>

</gee>

</ge>


(Code to write this instance XML file:)

for (int i=0;i< gelist2d.count;i++)//To write the element data from one entity to the XML file in turn

{

Geobj = (CGE) gelist2d[i];

The proportional reduction of the figure data in the entity is 4:1

Geobj.ptdrawstart.x = GEOBJ.PTDRAWSTART.X/4;

Geobj.ptdrawstart.y = GEOBJ.PTDRAWSTART.Y/4;

Geobj.ptdrawend.x = GEOBJ.PTDRAWEND.X/4;

Geobj.ptdrawend.y = GEOBJ.PTDRAWEND.Y/4;

Writer. WriteStartElement ("Gee");

Writer. WriteElementString ("Flagorder", GEObj.flagOrder.ToString ());

Writer. WriteElementString ("X", geobj.ptdrawstart.x.tostring ());

Writer. WriteElementString ("Y", geobj.ptdrawstart.y.tostring ());

Writer. WriteElementString ("X1", geobj.ptdrawend.x.tostring ());

Writer. WriteElementString ("Y1", geobj.ptdrawend.y.tostring ());

Writer. WriteElementString ("LW", GEObj.GElw.ToString ());

Writer. WriteElementString ("R", geobj.r.tostring ());

Writer. WriteElementString ("G", geobj.r.tostring ());

Writer. WriteElementString ("B", geobj.r.tostring ());

Writer. WriteEndElement ();

}

Writer. WriteEndElement ();

(Code to write this instance XML file:)

while (XTR. Read ())

{

Switch (XTR. NodeType)

{

Case XmlNodeType.Element:

{

if (XTR. Isstartelement ("GE"))

{

This. Gename[i] = xtr. GetAttribute ("name"). ToString ();//Get Entity Name

}

if (XTR. Isstartelement ("gee"))//start reading a graph

{}

if (XTR. Isstartelement ("Flagorder"))//flagorder

{

Switch (XTR. Readelementstring ("Flagorder"))//create different objects based on different command flags

{

Case "DrawLine":

This. Geobj = new Cline ();

This. Geobj.flagorder = "DrawLine";//Note To assign a value here

Break

Case "DrawRectangle":

This. Geobj = new Crectangle ();

This. Geobj.flagorder = "DrawRectangle";

Break

Case "DrawEllipse":

This. Geobj = new Cellipse ();

This. Geobj.flagorder = "DrawEllipse";

Break

Case "DrawArc":

This. Geobj = new Carc ();

This. Geobj.flagorder = "DrawArc";

Break

}

//////////////////////////////////////////////

}

if (XTR. Isstartelement ("X"))//ptdrawstart.x

{

This. Geobj.ptdrawstart.x = Int32.Parse (xtr. Readelementstring ("X"). ToString ());

}

if (XTR. Isstartelement ("Y"))//ptdrawstart.y

{

This. Geobj.ptdrawstart.y = Int32.Parse (xtr. Readelementstring ("Y"). ToString ());

}

if (XTR. Isstartelement ("X1"))//ptdrawend.x

{

This. Geobj.ptdrawend.x = Int32.Parse (xtr. Readelementstring ("X1"). ToString ());

}

if (XTR. Isstartelement ("Y1"))//ptdrawend.y

{

This. Geobj.ptdrawend.y = Int32.Parse (xtr. Readelementstring ("Y1"). ToString ());

}

if (XTR. Isstartelement ("LW"))//LW

{

This. GEOBJ.GELW = Int32.Parse (xtr. Readelementstring ("LW"). ToString ());

}

if (XTR. Isstartelement ("R"))//r

{

This. GEOBJ.R = Int32.Parse (xtr. Readelementstring ("R"). ToString ());

}

if (XTR. Isstartelement ("G"))//g

{

This. GEOBJ.G = Int32.Parse (xtr. Readelementstring ("G"). ToString ());

}

if (XTR. Isstartelement ("B"))//b

{

This. GEOBJ.B = Int32.Parse (xtr. Readelementstring ("B"). ToString ());

}

Break

}

Case xmlnodetype.endelement://Read the end of the graph element

{

if (XTR. Name.tostring (). Equals ("Gee"))

{

This. Gelist2d.add (Geobj)//a figure complete

}

Break

}

}

}

Gelist1d.add (GELIST2D)//Complete a gelist2d, add it to GELIST1D, read a metafile

Xtr. Close ();/key must be closed or the exclusive file will not be written

GELIST2D = new ArrayList ()//re-assign a region to GELIST2D

}



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.