从XML读取未知值类型

时间:2017-01-23 15:30:00

标签: c# xml linq

我有一个XML文件,其中标记值可以是int,float或string。如何使用Linq阅读本文?

更新#1: 我更新了XML以定义元素值的类型。但是,我将如何将其转换为SettingData类。

XML:

<Settings>
    <data type="uint" x="Some_Value_Here">200</data>  <!-- Read this in as an int -->
    <data type="float" x="Some_Value_Here">200.0</data>  <!-- Read this in as a float -->
</Settings>

到目前为止我所拥有的:

settings = xDoc.Descendants("Settings").Select(ele => new SettingData()
              {
                  Data = (string)ele.Attribute("data"),
                  Value = (string)ele == "" ? 0 : ele, // not sure what to put here
              }).ToList();

class SettingData
{
    private string data;
    public string Data
    {
        get { return data; }
        set { data = value; }
    }

    private Object value;  // What would the type of this var be if I will use the "type" attribute to determine the type of this var.
    public Object Value
    {
        get { return this.value; }
        set { this.value = value; }
    }
}

0 个答案:

没有答案