在读取XML时在IEnumerable中构建Try-Catch

cle*_*ech 1 c# xml try-catch

运行这个特定的代码块时,我在XML中遇到异常.我已经建立了一个try-catch,但是VS2010说"'WindowsFormsApplication1.Form1.GetDocumentsData(string)':并非所有代码路径都返回一个值".例外范围从空值到XML格式化异常.我只需要捕获它们并将它们记录到一个文件中(代码的一部分尚未完成).

C#代码:

    private static IEnumerable<object[]> GetDocumentsData(string folderPath = @"filepath")
    {
        try
        {
            return Directory.GetFiles(folderPath, "*.xml")
               .Select(XDocument.Load)
               .SelectMany(file => file.Descendants().Where(e => e.Name.LocalName == "FilingLeadDocument").Concat(file.Descendants().Where(e => e.Name.LocalName == "FilingConnectedDocument")))
               .Select(documentNode =>
               {
                   var receivedDateNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentReceivedDate");
                   var descriptionNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentDescriptionText");
                   var metadataNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentMetadata");
                   var registerActionNode = metadataNode.Elements().FirstOrDefault(e => e.Name.LocalName == "RegisterActionDescriptionText");

                   return new object[]
  {
       (string)documentNode.Parent.Parent.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentIdentification"),
       (DateTime?)receivedDateNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DateTime"),
       descriptionNode != null ? descriptionNode.Value.Trim() : string.Empty,
       registerActionNode != null ? registerActionNode.Value.Trim() : string.Empty
  };
               }).ToArray();
        }
        catch (Exception e)
        { }
    }
Run Code Online (Sandbox Code Playgroud)

Arr*_*ran 5

要修复实际的编译错误,只需IEnumerable在发生异常时返回空:

private static IEnumerable<object[]> GetDocumentsData(string folderPath = @"filepath")
{
    try
    {
        return Directory.GetFiles(folderPath, "*.xml")
           .Select(XDocument.Load)
           .SelectMany(file => file.Descendants().Where(e => e.Name.LocalName == "FilingLeadDocument").Concat(file.Descendants().Where(e => e.Name.LocalName == "FilingConnectedDocument")))
           .Select(documentNode =>
           {
               var receivedDateNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentReceivedDate");
               var descriptionNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentDescriptionText");
               var metadataNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentMetadata");
               var registerActionNode = metadataNode.Elements().FirstOrDefault(e => e.Name.LocalName == "RegisterActionDescriptionText");

              return new object[]
              {
                   (string)documentNode.Parent.Parent.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentIdentification"),
                   (DateTime?)receivedDateNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DateTime"),
                   descriptionNode != null ? descriptionNode.Value.Trim() : string.Empty,
                   registerActionNode != null ? registerActionNode.Value.Trim() : string.Empty
              };
           }).ToArray();
    }
    catch (Exception e)
    { 
        return Enumerable.Empty<object[]>();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我会质疑为什么你对这些错误感到"高兴" - >通常不应忽略异常.如果此处发生异常,您期望在链条上发生什么?