XML and entity classes (sequence and reverse sequence)

Source: Internet
Author: User

Two methods are provided:

1. xmldocument objects are deserialized into object objects through xmlreader

Xmlpath = system. Windows. Forms. application. startuppath + @ "\ books. xml ";

Xmldocument Doc = new xmldocument ();
Doc. Load (xmlpath );

Xmlreader reader = system. xml. xmlreader. Create (new system. Io. stringreader (Doc. outerxml ));

Xmlserializer xs = new xmlserializer (typeof (books ));
Books Config = Xs. deserialize (Reader) as books;

 

2. Reverse Sequence XML files through file streams

[xmlroot ("books")]
public class books: List
{< br> Public static books loadconfig (string file)
{< br> xmlserializer xs = new xmlserializer (typeof (books);
streamreader sr = new streamreader (File);
books Config = Xs. deserialize (SR) as books;
Sr. close ();

Return config;
}
Public void saveconfig (string file)
{
Xmlserializer xs = new xmlserializer (typeof (books ));
Streamwriter Sw = new streamwriter (File );
Xs. serialize (SW, this );
Sw. Close ();
}
}

 

Attached entity class:
[Xmltype ("book")] // used for books. Items
Public class Book
{
Private string _ bookname;
[Xmlattribute ("bookname")]
Public String bookname
{
Get {return this. _ bookname ;}
Set {This. _ bookname = value ;}
}
Private string _ author;
[Xmlattribute ("author")]
Public String author
{
Get {return this. _ author ;}
Set {This. _ author = value ;}
}
Private string _ date;
[Xmlattribute ("date")]
Public String date
{
Get {return this. _ date ;}
Set {This. _ date = value ;}
}
}

 

 

XML style:

<? XML version = "1.0" encoding = "UTF-8"?>
<Books>
<Book bookname = "C ++ language programming" author = "aaaa" date = "2009"> </book>
<Book bookname = "language programming" author = "BBBB" date = "2009"> </book>
<Book bookname = "language programming" author = "CCCC" date = "2009"> </book>
</Books>

 

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.