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

发布于 2024-10-19 19:19:04 字数 1027 浏览 3 评论 0原文

我有一些使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你如我软肋 2024-10-26 19:19:04

XML 保存字符串 - 如果您确定在 xml 中的哪些位置保留什么内容,您可以将其解析为正确的类型 - 就像 Tony Casale 为整数编写的那样。

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

像这样:

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

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

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:

<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.

请叫√我孤独 2024-10-26 19:19:04

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

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.

临走之时 2024-10-26 19:19:04

XElement 有一个用于显式 int 强制转换的运算符重载,因此您可能应该这样做

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 ) )

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

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 ) )
空名 2024-10-26 19:19:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文