Want to implement: C # reads the contents of the XML file into the list
XML file: Appattr.xml which, must be arrayofappattr(red part appattr for your entity class name)
<?XML version= "1.0" encoding= "Utf-8"?><arrayofappattrxmlns:xsd= "Http://www.w3.org/2001/XMLSchema"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"> <appattr> <Key>Filefolder</Key> <Value>C:/pwfileversion</Value> </appattr> <appattr> <Key>Calladdress</Key> <Value>pwfileversion://</Value> </appattr></arrayofappattr>
Entity class file: Appattr
[Serializable] // can't forget Public class appattr{ public appattr () {} public appattr (string String V) { = K; = V; } Public string Key; Public string Value;}
Serialization class: Xmlserialize
classxmlserialize{/// <summary> ///deserializing XML as a class instance/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "Xmlobj" ></param> /// <returns></returns> Public StaticT deserializexml<t> (stringxmlobj) {XmlSerializer Serializer=NewXmlSerializer (typeof(T)); using(StringReader reader =NewStringReader (xmlobj)) { return(T) serializer. Deserialize (reader); } } /// <summary> ///serializes a class instance to XML/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "obj" ></param> /// <returns></returns> Public Static stringSerializexml<t>(T obj) {using(StringWriter writer =NewStringWriter ()) { NewXmlSerializer (obj. GetType ()). Serialize ((TextWriter) writer, obj); returnwriter. ToString (); } }}View Code
Program run to read
Nullnew XmlSerializer (typeof(list<appattr>)); // _configpath for your XML configuration file location using New StreamReader (_configpath)) { as list<appattr>;}
Reference:
C#. NET parsing XML (simple instance)
C # parsing XML files