如何从Xml中检索值作为对象而不是字符串?

时间:2022-10-31 13:20:08

I have some code that parses an xml file using XDocument.

我有一些使用XDocument解析xml文件的代码。

It retrieves the values correctly but some of the values I save to xml is of type Object. So when I read these values from the xml, they come as string values, even though I cast them as objects:

它正确地检索值,但是我保存到xml的一些值是Object类型。因此,当我从xml中读取这些值时,它们作为字符串值出现,尽管我将它们转换为对象:

XDocument doc = XDocument.Load ( file );
XElement xe = doc.Element ( "EffectFile" );

xe.Elements ( "Options" ).Any ( )
    ? xe.Elements ( "Options" ).Select (
        o => o.Elements ( "Option" ).Select (
            n => ( object ) n.Value ).First ( ) )
    : null ) )

The value as it appears in the xml:

在xml中显示的值:

...
<Option>88</Option>
...

comes as "88", instead of Object 88.

以“88”的形式出现,而不是对象88。

I understand that when I cast a string to an Object, it will still be a string, but is there a way to make it an Object of the actual value (whatever that might be) but not a string?

我知道当我将一个字符串投射到一个对象时,它仍然是一个字符串,但是有没有一种方法可以使它成为一个实际值(无论它是什么)的对象,而不是一个字符串?

Because these are different, isn't it?:

因为它们是不同的,不是吗?

object value = 88;
object value = (object) "88";

and later when I use my options value stored as object[], I want to be able to do:

之后,当我使用存储为object[]的options值时,我希望能够做到:

int intValue = (int) value;

4 个解决方案

#1


4  

XML holds strings - if you are sure what you are keeping in what places in your xml, you can parse it to correct type - like Tony Casale wrote for ints.

XML保存字符串——如果您确定在XML中的什么地方保存了什么,您可以将其解析为正确的类型——就像Tony Casale为ints编写的那样。

Or if you can have any objects in some places, you'd have to add attribute which specifies to your xml reading code, to what type this node should be changed after reading.

或者,如果在某些地方可以有任何对象,则必须向xml读取代码中指定的属性添加属性,以便在读取后更改该节点的类型。

like this:

是这样的:

<option>
     <name>some name</name>
     <value type="int">123</value>
</option>
<option>
     <name>some other name</name>
     <value type="string">abcdefg</value>
</option>

Then in your code you will have to check which type to convert each string from value tag.

然后,在您的代码中,您将必须检查要从值标记中转换每个字符串的类型。

#2


3  

Yes those are different. You need to do:

是的,那些是不同的。你需要做的:

int intValue = int.Parse("88");

int intValue = int.Parse(“88”);

If you don't know what the source type is, you can also do:

如果你不知道源类型是什么,你也可以:

int intValue = Convert.ToInt32(someObject);

int intValue = Convert.ToInt32(someObject);

Which will convert anything to an int, if it can.

如果可以的话,它可以将任何东西转换成int类型。

#3


2  

XElement has an operator overload for explicit int cast, so you probably should be doing that

XElement有一个用于显式int cast的操作符重载,因此您可能应该这么做

XDocument doc = XDocument.Load ( file );
XElement xe = doc.Element ( "EffectFile" );

xe.Elements ( "Options" ).Any ( )
    ? xe.Elements ( "Options" ).Select (
        o => o.Elements ( "Option" ).Select (
            n => ( int) n).First ( ) )
    : null ) )

#4


2  

XML is text... it is normally returns values of nodes as text. As jbtule said you can use explicit casts (see http://msdn.microsoft.com/en-us/library/bb348319.aspx) for details.

XML文本…它通常以文本形式返回节点的值。正如jbtule所说,您可以使用显式类型转换(参见http://msdn.microsoft.com/en-us/library/bb348319.aspx)获取详细信息。

You may also consider using XML serialization instead of reading raw XML.

您还可以考虑使用XML序列化而不是读取原始XML。

#1


4  

XML holds strings - if you are sure what you are keeping in what places in your xml, you can parse it to correct type - like Tony Casale wrote for ints.

XML保存字符串——如果您确定在XML中的什么地方保存了什么,您可以将其解析为正确的类型——就像Tony Casale为ints编写的那样。

Or if you can have any objects in some places, you'd have to add attribute which specifies to your xml reading code, to what type this node should be changed after reading.

或者,如果在某些地方可以有任何对象,则必须向xml读取代码中指定的属性添加属性,以便在读取后更改该节点的类型。

like this:

是这样的:

<option>
     <name>some name</name>
     <value type="int">123</value>
</option>
<option>
     <name>some other name</name>
     <value type="string">abcdefg</value>
</option>

Then in your code you will have to check which type to convert each string from value tag.

然后,在您的代码中,您将必须检查要从值标记中转换每个字符串的类型。

#2


3  

Yes those are different. You need to do:

是的,那些是不同的。你需要做的:

int intValue = int.Parse("88");

int intValue = int.Parse(“88”);

If you don't know what the source type is, you can also do:

如果你不知道源类型是什么,你也可以:

int intValue = Convert.ToInt32(someObject);

int intValue = Convert.ToInt32(someObject);

Which will convert anything to an int, if it can.

如果可以的话,它可以将任何东西转换成int类型。

#3


2  

XElement has an operator overload for explicit int cast, so you probably should be doing that

XElement有一个用于显式int cast的操作符重载,因此您可能应该这么做

XDocument doc = XDocument.Load ( file );
XElement xe = doc.Element ( "EffectFile" );

xe.Elements ( "Options" ).Any ( )
    ? xe.Elements ( "Options" ).Select (
        o => o.Elements ( "Option" ).Select (
            n => ( int) n).First ( ) )
    : null ) )

#4


2  

XML is text... it is normally returns values of nodes as text. As jbtule said you can use explicit casts (see http://msdn.microsoft.com/en-us/library/bb348319.aspx) for details.

XML文本…它通常以文本形式返回节点的值。正如jbtule所说,您可以使用显式类型转换(参见http://msdn.microsoft.com/en-us/library/bb348319.aspx)获取详细信息。

You may also consider using XML serialization instead of reading raw XML.

您还可以考虑使用XML序列化而不是读取原始XML。