2012-11-30 2 views
2

case 개체 목록을 하나 이상의 XML 파일의 두 가지 정보로 채우려면 어떻게해야합니까?쿼리 결과를 반환하지 않습니다 - "열거 결과가 없습니다"

내가 해봤 모든 메서드는 0의 결과를 생산하고 변수를 확인하는 것은 "열거 결과가 없음"제공했다. 아래 코드 예제에서이 결과를 얻으려는 다양한 방법을 포함 시켰습니다 (주석으로 처리했습니다).

거기에 이미 답변이있는 경우 그 방향으로 나를 가리 키십시오. 나는 함께 제공된 설명에 감사 할 것이다.

다음은 코드 조각입니다 : 아래

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 파일입니다.

  • 여러 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> 
    

    생산에 해당 사례

답변

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]을 사용해야 할 수도 있습니다.? 난 아직도 어떤 결과를 얻을하지 않습니다 .. -

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

관련 문제