[英]Best way to present the user the XML values of a file?
我有一個XML文件,看起來很熟悉:
<root>
<carnumber>12</carnumber>
<carcolor>2</carcolor>
<cartype>5</cartype>
</root>
就像你看到我得到了一些帶有值/文本的元素。 例如,car元素可以取1到1000之間的值。但是carcolor元素可以取值為1到5,而cartype取值為1到10。
重要的是carcolor和cartype元素的值意味着什么。 carcolor“2”表示紅色,“1”表示藍色,依此類推。
所以我需要向用戶呈現值而不是值的真正含義。
我發現自己創建了一些代表具有有效值的元素的類,事情變得非常復雜,我不知道這是否是最佳方式。
我的一個朋友建議我使用XML序列化,因為我的XML文件是靜態的。 它永遠不會改變。
我的問題很簡單。 我只想知道你將如何解決這個問題。 我的想法包含代表XML元素的類,例如這個類中的cartype我有一對Dictonary。 這表示XML文件中的值,字符串是此值的含義。 我使用了很多Linq來導航和編輯值。
再次感謝!
這可以像你想要的那樣復雜或簡單。 但是,我會建議朋友使用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
字段之Type
的公開自定義枚舉並在內部序列化與其相關的數字來進一步改進此功能。 也許可以為汽車公開Color
enum並在內部存儲數值。
嘗試一下:
[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...
}
我添加了一些枚舉以允許演示。
您可以設置Car的值並將其序列化為xml字符串:
Car car = new Car();
car.Number = 1;
car.CarColor = CarColor.Blue;
car.CarType = CarType.Coupe;
string xml = car.Serialize();
並將xml字符串反序列化為汽車:
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);
為什么不格式化XML文件更像:
<root>
<carnumber code="2" name="John's Car"/>
<carcolor code="3" name="Red"/>
<cartype code="2" name="Hatchback"/>
</root>
我會在WPF中構建UI,而不是WinForms。 設置一個數據上下文,它將XML綁定為XML數據源,寫入類型轉換器以在內部和人類可讀值之間往返數據,將組合框綁定到各種元素,Bob是你的叔叔。
我認識到如果您不使用WPF,這個答案對您來說並不是很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.