Best way to present user XML file values?

I got an XML file familiar with this:

<root>
 <carnumber>12</carnumber>
 <carcolor>2</carcolor>
 <cartype>5</cartype>
</root>

As you can see, I have some Elements with values ​​/ texts in it. An automobile element, for example, can take values ​​from 1 to 1000. But a carcolor element can take values ​​from 1 to 5, and cartography from 1 to 10.

The important thing is that the values ​​of the carcolor and cartype elements mean something. carcolor "2" means red, "1" blue, etc.

Therefore, I need to present to the user not the values, but the real value of the values.

I found that I am creating several classes that represent elements with existing real values, and everything became really complex, and I don't know if this was / in the best way.

A friend of mine suggested I use XML serialization because my XML file is static. He will never change.

My question is simple. I just want to know how you solve this problem. My idea contains classes that represent an XML element, such as the file type in this class. I have a dictonary with a couple. This represents the values ​​in the XML file, and the string is the value of this value. And I use a lot of Linq to navigate and edit values.

Thanks again!

+3
source share
4 answers

Try:

[XmlRoot("root")]
public class Car
{
    private static XmlSerializer serializer = new XmlSerializer(typeof(Car));

    [XmlElement("carnumber")]
    public int Number { get; set; }

    [XmlElement("carcolor")]
    public int Color { get; set; }

    [XmlElement("cartype")]
    public int Type { get; set; }

    [XmlIgnore]
    public CarColor CarColor
    {
        get
        {
            return (CarColor)Color;
        }
        set
        {
            Color = (int)value;
        }
    }

    [XmlIgnore]
    public CarType CarType
    {
        get
        {
            return (CarType)Type;
        }
        set
        {
            Type = (int)value;
        }
    }

    public string CarColorString
    {
        get
        {
            return this.CarColor.ToString().Replace('_', ' ');
        }
    }

    public string CarTypeString
    {
        get
        {
            return this.CarType.ToString().Replace('_', ' ');
        }
    }

    public string Serialize()
    {
        StringBuilder sb = new StringBuilder();
        using (StringWriter writer = new StringWriter(sb))
        {
            serializer.Serialize(writer, this);
        }
        return sb.ToString();
    }

    public static Car Deserialize(string xml)
    {
        using (StringReader reader = new StringReader(xml))
        {
            return (Car)serializer.Deserialize(reader);
        }
    }
}

public enum CarColor
{
    Red = 1,
    Blue = 2,
    Green = 3,
    Light_Brown = 4
    // and so on...
}

public enum CarType
{
    Sedan = 1,
    Coupe = 2,
    Hatchback = 3,
    SUV = 4,
    Pickup_Truck = 5
    // and so on...
}

I have added several listings for the presentation.

You can set the Car values ​​and serialize it to an xml string:

Car car = new Car();
car.Number = 1;
car.CarColor = CarColor.Blue;
car.CarType = CarType.Coupe;
string xml = car.Serialize();

And deserialize the xml string into the car:

string example = 
@"<root>
 <carnumber>12</carnumber>
 <carcolor>2</carcolor>
 <cartype>5</cartype>
</root>";

Car car = Car.Deserialize(example);

CarColorString CarTypeString, , , .

Console.WriteLine(car.CarColorString);
Console.WriteLine(car.CarTypeString);
+1

, . , XML-, :

[XmlRoot("Car")]
public class Car
{
     public Car() 
     {
     }

     [XmlElement("Number")]
     public int Number { get; set; }

     [XmlElement("Color")]
     public int Color { get; set; }

     [XmlElement("Type")]
     public int Type { get; set; }
}

:

Car myCar = new Car();
myCar.Number = 1;
myCar.Color = 2;
myCar.Type = 3;

XmlSerializer s = new XmlSerializer(typeof(Car));
TextWriter w = new StreamWriter( @"c:\Car.xml" );
s.Serialize(w, myCar);
w.Close();

:

Car myCar;
TextReader r = new StreamReader("Car.xml");
myCar = (Car)s.Deserialize(r);
r.Close();

, Type , . , , Color .

+3

XML :

<root>
 <carnumber code="2" name="John Car"/>
 <carcolor code="3" name="Red"/>
 <cartype code="2" name="Hatchback"/>
</root>
0

WPF, WinForms. , XML XML, , , .

I understand that this answer is not very useful to you if you are not using WPF.

0
source

Source: https://habr.com/ru/post/1728929/


All Articles