当元素缺少其值时解析 xml
我正在尝试解析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在解析时遇到问题,尝试显式解析可能是值得的。例如
,或者甚至使用 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
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.