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>