当元素缺少其值时解析 xml

发布于 2024-10-11 02:03:00 字数 1245 浏览 1 评论 0原文

我正在尝试解析 xml 格式的一些货币数据。 下面的代码不起作用,但是当我将其全部解析为字符串时,它确实起作用。

            CurrencyName = (string)d.Element("CurrencyName"),
            ForexBuying = ((decimal?)d.Element("ForexBuying")),
            ForexSelling = ((decimal?)d.Element("ForexSelling")),
            BanknoteBuying = ((decimal?)d.Element("BanknoteBuying")),
            BanknoteSelling = ((decimal?)d.Element("BanknoteSelling")),
            CrossRateEuro = ((decimal?)d.Element("CrossRateEuro")),
            CrossRateUSD = ((decimal?)d.Element("CrossRateUSD"))

所有元素中只有CurrencyName存在,有时我们有这样的元素 ,某些节点根本不携带 BanknoteBuying 元素。 奇怪的是我收到日期/时间解析数据错误。简而言之,将其全部转换为字符串是可行的,但转换为适当的可为空数据类型则不行,数据格式正确,并且本地区域设置正确以解析十进制数据。

  <Currency Kod="RUB" CurrencyCode="RUB">
<Unit>1</Unit>
<Isim>RUS RUBLESİ</Isim>
<CurrencyName>RUSSIAN ROUBLE</CurrencyName>
<ForexBuying>0.05011</ForexBuying>
<ForexSelling>0.05077</ForexSelling>
<BanknoteBuying></BanknoteBuying>
<BanknoteSelling></BanknoteSelling>
<CrossRateUSD>30.5655</CrossRateUSD>
<CrossRateOther></CrossRateOther>

I am trying to parse some currency data in xml format.
Below code does not work, but when I parse it all as string, it does work.

            CurrencyName = (string)d.Element("CurrencyName"),
            ForexBuying = ((decimal?)d.Element("ForexBuying")),
            ForexSelling = ((decimal?)d.Element("ForexSelling")),
            BanknoteBuying = ((decimal?)d.Element("BanknoteBuying")),
            BanknoteSelling = ((decimal?)d.Element("BanknoteSelling")),
            CrossRateEuro = ((decimal?)d.Element("CrossRateEuro")),
            CrossRateUSD = ((decimal?)d.Element("CrossRateUSD"))

Only CurrencyName exists in all elements, sometimes we have elements like
<BanknoteBuying></BanknoteBuying>, some nodes do not carry the BanknoteBuying element at all.
Odd thing is I am getting a date/time parsing data error. So in short, casting it all to string works, but casting to appropriate nullable data type does not, data is well formed, and the local region is set correct to parse the decimal data.

  <Currency Kod="RUB" CurrencyCode="RUB">
<Unit>1</Unit>
<Isim>RUS RUBLESİ</Isim>
<CurrencyName>RUSSIAN ROUBLE</CurrencyName>
<ForexBuying>0.05011</ForexBuying>
<ForexSelling>0.05077</ForexSelling>
<BanknoteBuying></BanknoteBuying>
<BanknoteSelling></BanknoteSelling>
<CrossRateUSD>30.5655</CrossRateUSD>
<CrossRateOther></CrossRateOther>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

丶情人眼里出诗心の 2024-10-18 02:03:00

如果您在解析时遇到问题,尝试显式解析可能是值得的。例如

Decimal.Parse(d.Element("CrossRateUSD"));

,或者甚至使用 TryParse 可能会产生更多信息为你介绍一下。

还有一件事要尝试,我在写这篇文章时刚刚注意到,我认为您正在寻找的是元素的值,而不是元素本身。在不知道您的 XML 格式的情况下,我不能肯定地说 100%,但请尝试 d.Element("CrossRateUSD").Value。

更新:添加了 TryParse 的 MSDN 链接。

If you are having troubles with parsing, it might be worth it to try an explicit parse. For example

Decimal.Parse(d.Element("CrossRateUSD"));

Or even with TryParse might yeild some more info about it for you.

One more thing to try out, that I just noticed as I was writing this up is that what I think you are looking for is the value form the element, not the element itself. Without knowing your XML format, I can'T say 100% for sure, but try d.Element("CrossRateUSD").Value instead.

UPDATE: Added link to MSDN for TryParse.

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