Where to store constant objects that will be used in my application

前端 未结 5 1953
[愿得一人]
[愿得一人] 2021-01-23 05:00

Lets say I have an item, which has fields(properties)

  1. Location
  2. Average value
  3. Usability

And I have 10-15 items

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-23 05:48

    Your options will be:

    1. XML - one of your tags actually
    2. Database
    3. Binary File

    Store the objects and the read them in your code.

    Write XML code example:

    public void WriteXML()
    {
        Book overview = new Book();
        overview.title = "Serialization Overview";
        System.Xml.Serialization.XmlSerializer writer = 
            new System.Xml.Serialization.XmlSerializer(typeof(Book));
    
        System.IO.StreamWriter file = new System.IO.StreamWriter(
            @"c:\temp\SerializationOverview.xml");
        writer.Serialize(file, overview);
        file.Close();
    }
    

    Read XML code example:

    public void Read(string  fileName)
    {
        XDocument doc = XDocument.Load(fileName);
    
        foreach (XElement el in doc.Root.Elements())
        {
            Console.WriteLine("{0} {1}", el.Name, el.Attribute("id").Value);
            Console.WriteLine("  Attributes:");
            foreach (XAttribute attr in el.Attributes())
                Console.WriteLine("    {0}", attr);
            Console.WriteLine("  Elements:");
    
            foreach (XElement element in el.Elements())
                Console.WriteLine("    {0}: {1}", element.Name, element.Value);
        }
    }
    

提交回复
热议问题