2012-11-30 183 views
2

如何用一个或多个xml文件中的两条信息填充case对象的列表?查询永不返回结果 - “枚举没有结果”

每次我试过的方法已经产生结果为零,并检查变量提供“枚举没有结果”。在我的代码示例如下,我已经包括(注释掉)我试图用来实现这个结果的各种方法。

如果已经有这方面的答案,请指出我的方向。我也很感激任何附带的解释。

以下是代码片段:

private void gatherInfo(ref List<Case> Cases) 
{ 
    List<string> XMLFileNames = new List<string>(); 

    foreach (string caseID in txtbxCases.Lines) 
    { 
     Cases.Add(new Case(caseID)); 
    } 

    XMLFileNames.AddRange(Directory.EnumerateFiles(txtbxXMLDirectory.Text, "*", SearchOption.AllDirectories).Select(Path.GetFileName)); 

    string currentFileName = ""; 

    for (int i = XMLFileNames.Count - 1; i >= 0; i--) 
    { 
     currentFileName = XMLFileNames[i]; 
     if (currentFileName.Substring(currentFileName.Length - 4, 4) != ".xml") 
     { 
      XMLFileNames.RemoveAt(i); 
     } 
    } 

    // Start of Area that doesn't work correctly 
    for (int i = XMLFileNames.Count - 1; i>=0; i--) 
    { 
     currentFileName = XMLFileNames[i]; 
     //XElement currentDoc = XElement.Load(txtbxXMLDirectory.Text + "\\" + currentFileName); 
     XDocument currentDoc = XDocument.Load(txtbxXMLDirectory.Text + "\\" + currentFileName); 
     XElement currentDocElements = XElement.Parse(currentDoc.ToString()); 

     for (int i2 = Cases.Count - 1; i2>=0; i2--) 
     { 
      string currentCaseID = Cases[i].GetCaseID(); 

      IEnumerable<XElement> currentCase = 
       from el in currentDocElements.Descendants("Document"); 
       where (string)el.Element("CaseNumber") == currentCaseID 
       select el; 
      /////////////////////////////////////////////////////////// 
      //var currentCase = 
      // from el in currentDocElements.Descendants("IndexFields") 
      // where (string)el.Element("CaseNumber") == currentCaseID 
      // select el; 
      /////////////////////////////////////////////////////////// 
      //var currentCase = currentDocElements.Descendants("IndexFields") 
      // .where(x => x.Element("IndexField").Value == currentCaseID); 
      /////////////////////////////////////////////////////////// 

      foreach (XElement el in currentCase) 
      { 
       Cases[i].AddFile(el.Element("SRCFilename").Value, el.Element("XMLFilename").Value); 
      } 
     } 
    } 

    //foreach (string filename in XMLFileNames) 
    //{ 
    // XElement currentDoc = XElement.Load(txtbxXMLDirectory.Text + "\\" + filename); 
    // 
    // foreach (var caseID in Cases) 
    // { 
    //  IEnumerable<XElement> currentCase = 
    //   from el in currentDoc.ElementsAfterSelf("IndexFields")//.Elements("IndexFields") 
    //   //where (string)el.Element("CaseNumber") == caseID.GetCaseID() 
    //   select el; 
    // 
    //  foreach (XElement el in currentCase) 
    //  { 
    //   caseID.AddFile(el.Element("SRCFilename").Value, el.Element("XMLFilename").Value); 
    //  } 
    // } 
    //} 
} 

下面是包括所有的嵌套的生产 XML文件的示例XML文件。

<ImportSession> 
<Batches> 
<Batch BatchClassName="CaseFolder_XML"> 
<Documents> 
<Document FormTypeName="Doc XML Form Type"> 
<IndexFields> 
<IndexField Name="CaseNumber" Value="1"/> 
<IndexField Name="XMLFilename" Value="aaa.xml"/> 
<IndexField Name="SRCFilename" Value="aaa.pdf"/> 
</IndexFields> 
<Pages> 
<Page ImportFileName="c:\aaa.pdf"/> 
</Pages> 
</Document> 
<Document FormTypeName="Doc XML Form Type"> 
<IndexFields> 
<IndexField Name="CaseNumber" Value="2"/> 
<IndexField Name="XMLFilename" Value="aaa.xml"/> 
<IndexField Name="SRCFilename" Value="aab.pdf"/> 
</IndexFields> 
<Pages> 
<Page ImportFileName="c:\aab.pdf"/> 
</Pages> 
</Document> 
<Document FormTypeName="Doc XML Form Type"> 
<IndexFields> 
<IndexField Name="CaseNumber" Value="3"/> 
<IndexField Name="XMLFilename" Value="aaa.xml"/> 
<IndexField Name="SRCFilename" Value="aac.pdf"/> 
</IndexFields> 
<Pages> 
<Page ImportFileName="c:\aac.pdf"/> 
</Pages> 
</Document> 
<Document FormTypeName="Doc XML Form Type"> 
<IndexFields> 
<IndexField Name="CaseNumber" Value="4"/> 
<IndexField Name="XMLFilename" Value="aaa.xml"/> 
<IndexField Name="SRCFilename" Value="aad.pdf"/> 
</IndexFields> 
<Pages> 
<Page ImportFileName="c:\aad.pdf"/> 
</Pages> 
</Document> 
<Document FormTypeName="Doc XML Form Type"> 
<IndexFields> 
<IndexField Name="CaseNumber" Value="1"/> 
<IndexField Name="XMLFilename" Value="aaa.xml"/> 
<IndexField Name="SRCFilename" Value="aae.pdf"/> 
</IndexFields> 
<Pages> 
<Page ImportFileName="c:\aae.pdf"/> 
</Pages> 
</Document> 
<Document FormTypeName="Doc XML Form Type"> 
<IndexFields> 
<IndexField Name="CaseNumber" Value="1"/> 
<IndexField Name="XMLFilename" Value="aaf.xml"/> 
<IndexField Name="SRCFilename" Value="aab.pdf"/> 
</IndexFields> 
<Pages> 
<Page ImportFileName="c:\aaf.pdf"/> 
</Pages> 
</Document> 
</Documents> 
</Batch> 
</Batches> 
</ImportSession> 

以下生产为真:

  • 多个XML文件来检查
  • 的情况可能多次出现在一个XML文件
  • 同样的文件可以被添加到多个案例

回答

0
var xDoc = XDocument.Load("a.xml"); 
var allDocs = xDoc.Descendants("Document") 
       .Select(doc=>new{ 
        FormTypeName = doc.Attribute("FormTypeName").Value, 
        IndexFields = doc.Descendants("IndexField") 
            .ToDictionary(x=>x.Attribute("Name").Value, 
                x=>x.Attribute("Value").Value) 
       }) 
       .ToList(); 

您还可以使用XPath

string xpath = "//Document[IndexFields/IndexField[@Name='CaseNumber' and @Value='3']]"; 
var docElement = xDoc.XPathSelectElement(xpath); 
1

也许你应该阅读Cases[i2]而不是Cases[i]

for (int i2 = Cases.Count - 1; i2>=0; i2--) 
{  //^counter for iterating over "Cases" 

    string currentCaseID = Cases[i2].GetCaseID(); 
           //^use correct counter here 

    IEnumerable<XElement> currentCase = 
     from el in currentDocElements.Descendants("Document"); 
     where (string)el.Element("CaseNumber") == currentCaseID 
     select el; 

    foreach (XElement el in currentCase) 
    { 
     Cases[i2].AddFile(el.Element("SRCFilename").Value, el.Element("XMLFilename").Value); 
      //^and here 
    } 
+0

D'哦......这绝对是一个错误......试过了,仍然没有去 - 我还没有得到任何结果.. “枚举没有结果”是我的祸根! – Brad