如何使用一个或多个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>
生产中如下:
答案 0 :(得分: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
}
答案 1 :(得分: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);