C # parsing XML files

Source: Internet
Author: User

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

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.