开发者

How would I deserialize some class from XML (file), and vice versa?

开发者 https://www.devze.com 2023-04-05 07:08 出处:网络
I have the following class and data members (plus irrelevant methods) I am new to XML and .NET (that excuse is getting old though) and have spent a couple of days reading the MSDN entries (and whateve

I have the following class and data members (plus irrelevant methods) I am new to XML and .NET (that excuse is getting old though) and have spent a couple of days reading the MSDN entries (and whatever google turned up) for first XMLReader, then XMLDocument and now XDocument, XElement and XNode but am no closer to a concrete solution to serializing my class (perhaps I need to study serialization in .NET in more depth). To get me started I have some data in an XML file that I want to read (although it prolly is in the wrong format) in to initialize a class to initialize my application. The configuration class is as follows:

class 开发者_JS百科IWantToFile
{
    class DirsAndFiles
    {
        public List<string> Suffixes;
        public string Dir;
    }
    enum OPOptsEnum
    {
        op1Description, op2Description, op3Description, op4Description,
        op5Description, op6Description, op7Description, op8Description,
    };
    List<DirsAndFiles> ProjectDirs;
    bool[] OPOpts = new bool[(int)OPOptsEnum.op8Description + 1];
    bool otherOpt;
}

Observing the one to one and one to many relationships therein (eg List<DirsAndFiles> ProjectDirs) can someone please give concise methods to read and write this data to a file? It would greatly assist my development in these fields.

I've got as far as:

        if (File.Exists(SPECFILENAME)) {
            XDocument xdoc = XDocument.Load(SPECFILENAME);
            //Ummm.....
        }

but then my lack of .NET XML and Linq exeperience fail me.


I think you might want to use the XmlSerializer, which 'Serializes and deserializes objects into and from XML documents'?

See How to serialize an object to XML by using Visual C# for sample code.


[Serializable]
public class MyObject
{
    public string SerializeMe { get; set; }

    [XmlIgnore]
    public string DONTSerializeMe { get; set; }
}

Helper....

public static class SerializerHelper<T>
{
    public static string Serialize(T myobject)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        StringWriter stringWriter = new StringWriter();
        xmlSerializer.Serialize(stringWriter, myobject);
        string xml = stringWriter.ToString();

        return xml;
    }
    public static T Deserialize(string xml)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        StringReader stringReader = new StringReader(xml);
        return (T)xmlSerializer.Deserialize(stringReader);
    }
}

Usage

MyObject myObject = new MyObject();
string xml = SerializerHelper<MyObject>.Serialize(myObject);
MyObject DeserializedObject = SerializerHelper<MyObject>.Deserialize(xml);
0

精彩评论

暂无评论...
验证码 换一张
取 消