如何从 Xml 中检索值作为对象而不是字符串?
我有一些使用 XDocument
解析 xml 文件的代码。
它正确检索值,但我保存到 xml 的一些值是 Object
类型。因此,当我从 xml 中读取这些值时,它们以 string
值的形式出现,即使我将它们转换为对象:
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 ) )
xml 中显示的值:
...
<Option>88</Option>
...
为 "88",而不是
Object 88
。
我知道当我将 string
转换为 Object
时,它仍然是一个 string
,但是有没有办法让它成为 string
实际值(无论是什么)的 code>Object 但不是 string
?
因为它们是不同的,不是吗?:
object value = 88;
object value = (object) "88";
稍后当我使用存储为 object[]
的 options
值时,我希望能够执行以下操作:
int intValue = (int) value;
I have some code that parses an xml file using XDocument
.
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:
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:
...
<Option>88</Option>
...
comes as "88"
, instead of Object 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:
int intValue = (int) value;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
XML 保存字符串 - 如果您确定在 xml 中的哪些位置保留什么内容,您可以将其解析为正确的类型 - 就像 Tony Casale 为整数编写的那样。
或者,如果您可以在某些地方拥有任何对象,则必须添加属性,该属性指定您的 xml 读取代码,读取后该节点应更改为什么类型。
像这样:
然后在您的代码中,您必须检查将值标记中的每个字符串转换为哪种类型。
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.
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.
like this:
Then in your code you will have to check which type to convert each string from value tag.
是的,这些是不同的。你需要这样做:
int intValue = int.Parse("88");
如果你不知道源类型是什么,你也可以这样做:
int intValue = Convert.ToInt32 (someObject);
如果可以的话,它会将任何内容转换为
int
。Yes those are different. You need to do:
int intValue = int.Parse("88");
If you don't know what the source type is, you can also do:
int intValue = Convert.ToInt32(someObject);
Which will convert anything to an
int
, if it can.XElement 有一个用于显式 int 强制转换的运算符重载,因此您可能应该这样做
XElement has an operator overload for explicit int cast, so you probably should be doing that
XML 是文本...它通常以文本形式返回节点的值。正如 jbtule 所说,您可以使用显式强制转换(请参阅 http://msdn.microsoft.com /en-us/library/bb348319.aspx)了解详细信息。
您还可以考虑使用 XML 序列化而不是读取原始 XML。
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.
You may also consider using XML serialization instead of reading raw XML.