我有一个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
}我的问题是,当我点击第1节按钮时,如何才能显示所有来自第1节的子项目,其他部分的子元素也是一样的?
发布于 2015-05-07 13:36:18
对于那些感兴趣的人,我得到的结果是:
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
}发布于 2015-03-23 14:14:17
您必须添加对节点属性值的检查。
例如,对于只获得第1节的内容,应该可以这样做:
var section1 = (from m in doc.Root.Descendants("AssessmentSections").Descendants("AssessmentSection")
where m.Attribute("sectionNameID").Value == "SECTION_ONE"
select m);发布于 2015-03-23 14:40:19
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
}我做了这样的事,希望能帮上忙
https://stackoverflow.com/questions/29212311
复制相似问题