阅读XML的另一部分,而不在表中创建新行

时间:2014-04-21 16:45:45

标签: c# xml ienumerable

在下面的代码中,我想知道如何从“ReviewLeadDocument”/“LowestConnectedDocument”部分获取序列ID,而不向数据表添加新行。到目前为止,我必须使用一个完全不同的表来存储这些信息,这引起了很大的麻烦。列出的第一个序列是从'FilingLeadDocument'/'FilingConnectedDocument'中读取的。如何在没有迭代更多行的情况下进入“已审核”部分中的第二个“DocumentSequenceID”?

private static IEnumerable<object[]> GetDocumentsData(string folderPath = @"filepath")
{

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")
.Concat(file.Descendants().Where(e => e.Name.LocalName == "ReviewedLeadDocument")
.Concat(file.Descendants().Where(e => e.Name.LocalName == "ReviewedConnectedDocument")))))
 .Select(documentNode =>
 {
     try
     {
var receivedDateNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentReceivedDate");
var SequenceNode = documentNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DocumentSequenceID");
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"),
SequenceNode != null ? SequenceNode.Value.Trim() : string.Empty,
(DateTime?)receivedDateNode.Elements().FirstOrDefault(e => e.Name.LocalName == "DateTime"),
descriptionNode != null ? descriptionNode.Value.Trim() : string.Empty,
registerActionNode != null ? registerActionNode.Value.Trim() : string.Empty
 };
     }
     catch (Exception e)
     {
 //Log.error("");
 return new object[] { };
     }
 }).ToArray();
}

XML Snipit显示序列ID的两个位置: (这是文件连接文件部分)

 <FilingConnectedDocument s:id="DOC00010" s:metadata="# Pages=2">
        <nc:DocumentApplicationName>application/pdf</nc:DocumentApplicationName>
        <nc:DocumentDescriptionText s:id="Motions">Motion</nc:DocumentDescriptionText>
        <nc:DocumentFileControlID s:id="FileInputId">111</nc:DocumentFileControlID>
        <nc:DocumentFileControlID s:id="Rule6PublicAnswer">-1</nc:DocumentFileControlID>
        <nc:DocumentFileControlID s:id="Rule6ConfidentialAnswer">-1</nc:DocumentFileControlID>
        <nc:DocumentFileControlID s:id="TypeOfConfidentialDocument">-1</nc:DocumentFileControlID>
        <nc:DocumentPostDate>
          <nc:DateTime>2014-04-09T15:24:06.8579836-04:00</nc:DateTime>
        </nc:DocumentPostDate>
        <nc:DocumentReceivedDate>
          <nc:DateTime>2014-04-09T15:24:05.797-04:00</nc:DateTime>
        </nc:DocumentReceivedDate>
        **<nc:DocumentSequenceID>10</nc:DocumentSequenceID>**
        <ecf:DocumentMetadata>

(这是已审核的已连接文档部分)

<ecf:ReviewedConnectedDocument s:id="REVIEWEDDOC00003">
    <nc:DocumentFiledDate>
      <nc:DateTime>2014-04-09T00:00:00</nc:DateTime>
    </nc:DocumentFiledDate>
    <nc:DocumentPostDate>
      <nc:DateTime>2014-04-09T15:24:06.857</nc:DateTime>
    </nc:DocumentPostDate>
    <nc:DocumentReceivedDate>
      <nc:DateTime>2014-04-09T15:24:05.797</nc:DateTime>
    </nc:DocumentReceivedDate>
    **<nc:DocumentSequenceID>3</nc:DocumentSequenceID>**

1 个答案:

答案 0 :(得分:1)

要获取所选内容中第一个和第二个元素的序列ID,请执行以下操作:

var matchingElements = documentNode.Elements().Where(e => e.Name.LocalName == "DocumentSequenceID");

var firstSequenceNode = matchingElements.FirstOrDefault();
var secondSequenceNode = matchingElements.ElementAtOrDefault(1);

我希望这就是你想要的。