我有一个包含以下信息的xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<AssessmentForm title="MACHINE-NAME Assessment">
<Title>MACHINE-NAME Assessment</Title>
<AssessmentSections>
<AssessmentSection sectionNameID="SECTION_ONE" sectionGroupID="0">
<SectionName>SECTION_NUMBER_1</SectionName>
<SectionDescription>Description of Section to go here</SectionDescription>
<SectionItems>
<SectionItem itemNameID="ITEM_ONE" itemID="0" itemGroupID="0">
<ItemName>Item One</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
<SectionItem itemNameID="ITEM_TWO" itemID="1" itemGroupID="0">
<ItemName>Item Two</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
<SectionItem itemNameID="ITEM_THREE" itemID="2" itemGroupID="0">
<ItemName>Item Three</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
</SectionItems>
</AssessmentSection>
<AssessmentSection sectionNameID="SECTION_TWO" sectionGroupID="1">
<SectionName>SECTION_NUMBER_2</SectionName>
<SectionDescription>Description of Section to go here</SectionDescription>
<SectionItems>
<SectionItem itemNameID="ITEM_ONE" itemID="0" itemGroupID="1">
<ItemName>Item One</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
<SectionItem itemNameID="ITEM_TWO" itemID="1" itemGroupID="1">
<ItemName>Item Two</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
<SectionItem itemNameID="ITEM_THREE" itemID="2" itemGroupID="1">
<ItemName>Item Three</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
</SectionItems>
</AssessmentSection>
<AssessmentSection sectionNameID="SECTION_THREE" sectionGroupID="2">
<SectionName>SECTION_NUMBER_2</SectionName>
<SectionDescription>Description of Section to go here</SectionDescription>
<SectionItems>
<SectionItem itemNameID="ITEM_ONE" itemID="0" itemGroupID="2">
<ItemName>Item One</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
<SectionItem itemNameID="ITEM_TWO" itemID="1" itemGroupID="2">
<ItemName>Item Two</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
<SectionItem itemNameID="ITEM_THREE" itemID="2" itemGroupID="2">
<ItemName>Item Three</ItemName>
<Significance>Description of Item to go here</Significance>
<Result />
</SectionItem>
</SectionItems>
</AssessmentSection>
</AssessmentSections>
</AssessmentForm>
通过执行以下操作,我已设法使用LINQ获取子元素:
var menu = (from m in doc.Root.Descendants("AssessmentSections").Descendants("AssessmentSection")
select new
{
sectionID = Convert.ToInt32(m.Attribute("sectionGroupID").Value),
sectionName = m.Attribute("sectionNameID").Value,
sectionItems = (from sub in doc.Descendants("SectionItems").Descendants("SectionItem")
where Convert.ToInt32(sub.Attribute("itemGroupID").Value).Equals(Convert.ToInt32(m.Attribute("sectionGroupID").Value))
select new
{
itemGroupID = Convert.ToInt32(sub.Attribute("itemGroupID").Value),
itemID = Convert.ToInt32(sub.Attribute("itemID").Value),
itemName = sub.Element("ItemName").Value,
itemResult = sub.Element("Result").Value
}).ToArray()
}).ToArray();
然而,我的问题是为正确的父级获取正确的子元素。我目前的代码:
foreach (var m in menu)
{
//display m in button array
}
foreach (var sub in m.sectionItems)
{
//display sub in textbox & combobox array
}
我的问题是,当我点击SECTION 1 buttton时,如何显示仅来自第1部分的所有子项目,同样适用于其他部分的子元素?
答案 0 :(得分:1)
您必须添加检查节点的属性值。
例如,只有获得第1部分,这应该有效:
var section1 = (from m in doc.Root.Descendants("AssessmentSections").Descendants("AssessmentSection")
where m.Attribute("sectionNameID").Value == "SECTION_ONE"
select m);
答案 1 :(得分:0)
XElement sectionOne = doc.Descendants(namespace + "AssessmentSection").First(x => x.Attribute("sectionNameID").Value.Equals("SECTION_ONE"));
IEnumerable<XElement> elements = sectionOne.Elements(namespace + "SectionItem");
foreach (XElement element in elements)
{
//Do something
}
我做了类似的事情,希望有所帮助
答案 2 :(得分:0)
对于那些感兴趣的人,我得到的结果是:
foreach (var m in menu)
{
//display button array
}
foreach (var sectionItem in m.sectionItems.Where(x => x.itemGroupID == m.sectionID))
{
//display sub in textbox & combobox array
}