运行此特定代码块时,我在 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)
{ }
}