C# Linq到Xml-输入字符串

C# Linq到Xml-输入字符串,c#,linq-to-xml,C#,Linq To Xml,我正在使用LINQtoXML解析来自遗留系统的一些Xml消息。其中一条消息以名称/值对的形式出现。因此,我按名称进行查找,然后尝试获取等效值。但是,当值为空()时,我的代码抛出错误输入字符串的格式不正确。 我正试图找出解决这个问题的最佳方法。任何建议都将不胜感激(尝试使用可为null的int类型int填充属性?) 代码示例: myRecord.myField= xdoc.Descendants("Information") .Where(x =&g

我正在使用LINQtoXML解析来自遗留系统的一些Xml消息。其中一条消息以名称/值对的形式出现。因此,我按名称进行查找,然后尝试获取等效值。但是,当值为空(
)时,我的代码抛出错误
输入字符串的格式不正确。

我正试图找出解决这个问题的最佳方法。任何建议都将不胜感激(尝试使用可为null的int类型int填充属性?)

代码示例:

myRecord.myField= xdoc.Descendants("Information")
                        .Where(x => (string)x.Element("Name") == "myField")
                        .Select(x => (int?)x.Element("Value")).FirstOrDefault();
XML代码段:

    <Information>
      <Name>myField</Name>
      <Value />
    </Information>

麦菲尔德
始终感谢反馈/输入

谢谢


S

当元素为空时,则其值为
字符串。空
,不能解析为整数。因此,您应该手动处理此情况:

myRecord.myField = xdoc.Descendants("Information")
                       .Where(x => (string)x.Element("Name") == "myField")
                       .Select(x => x.Element("Value"))
                       .Select(v => (v == null || v.IsEmpty) ? null : (int?)v)
                       .FirstOrDefault();
已经提供了正确的答案,但我认为多做一点解释会有帮助

整件事都是关于你的。注意这个例子,看看发生了什么:

XElement element = null;
// returns null
int? value = (int?)element;

element = new XElement("test", 1);
// returns 1
value = (int?)element;

element = new XElement("test");
// throws FormatException
value = (int?)element;
(int?)xElementInstance
仅返回
null
,其中元素为
null
。否则,每当
XElement.Value
不是整数时,就会处理int解析,这会引发异常(就像在out的情况下,当没有
Value
时,就像
int.Parse(String.Empty)

在转换之前,必须检查
是否设置了XElement
以及
XElement是否有值

if (element == null)
    return null;
else if (element.IsEmpty)
    return null
else if (string.IsNullOrEmpty(element.Value))
    return null
else
    return (int?)element;
使用内联语句可以轻松完成的操作:

(element == null || element.IsEmpty || string.IsNullOrEmpty(element.Value) ? null : (int?)element)
总而言之,以下代码做了您想要做的事情-在元素没有值时从XElement中获取
int?

element = new XElement("test");
// returns null
value = element == null || element.IsEmpty || string.IsNullOrEmpty(element.Value) ? null : (int?)element;
这应该起作用:

public static class Extensions
{
   public static int? ToInt32(this XElement element)
   {
      if (element == null) return null;
      if (element.IsEmpty) return null;

      // If the element is declared as <Value></Value>,
      // IsEmpty will be false, but the value will be an empty string:
      if (string.IsNullOrEmpty(element.Value)) return null;

      return XmlConvert.ToInt32(element.Value);
   }
}

myRecord.myField = doc.Descendants("Information")
   .Where(x => (string)x.Element("Name") == "myField")
   .Select(x => x.Element("Value").ToInt32()).FirstOrDefault();
公共静态类扩展
{
公共静态int?ToInt32(此元素)
{
if(element==null)返回null;
if(element.IsEmpty)返回null;
//如果元素声明为,
//IsEmpty将为false,但值将为空字符串:
if(string.IsNullOrEmpty(element.Value))返回null;
返回XmlConvert.ToInt32(element.Value);
}
}
myRecord.myField=文档子体(“信息”)
.Where(x=>(字符串)x.Element(“名称”)==“myField”)
.Select(x=>x.Element(“Value”).ToInt32()).FirstOrDefault();

将在没有此类元素时抛出NullReferenceException(这里可能不是这种情况,但值得一提)@MarcinJuraszek nope,它不会,此条件将处理缺少的元素
(v==null | | v.IsEmpty)
@MarcinJuraszek实际上是:)你的评论出现在我更新答案之后:)如果你使用
XElement.Parse(“”
,那么
IsEmpty
将返回
false
,但是
值将返回一个空字符串,转换将抛出
FormatException
。你说得对!我不知道,所以非常感谢!更新了我的答案。