How do I populate a list of case objects with two pieces of information from one or more xml files?
Every method I’ve tried has produced zero results and checking the variable gives “Enumeration yielded no results”. In my code example below, I have included (commented out) various methods I’ve tried to use to achieve this results.
If there is already an answer out there for this, please point me in that direction. I would appreciate any accompanying explanation as well.
Here are the code snippets:
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);
// }
// }
//}
}
Below is an example XML file that includes all of the nesting that the production XML files have.
<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>
The following are true in production:
- Multiple xml files to check
- A case may show up multiple times in an xml file
- The same files can be added to multiple cases
You can also use XPath