Hi I have the following xml file:
<?xml version="1.0" encoding="utf-8"?>
<Resources>
<Category Cause="ONE">
<Resource Component="AAA" Name="cBackgroundTextA" />
<Resource Component="BBB" Name="cBackgroundTextB" />
<Resource Component="CCC" Name="cBackgroundTextC" />
</Category>
<Category Cause="TWO">
<Resource Component="DDD" Name="cBackgroundTextD" />
<Resource Component="EEE" Name="cBackgroundTextE" />
<Resource Component="FFF" Name="cBackgroundTextF" />
</Category>
<Category Cause="THREE">
<Resource Component="GGG" Name="cBackgroundTextG" />
<Resource Component="HHH" Name="cBackgroundTextH" />
<Resource Component="III" Name="cBackgroundTextI" />
</Category>
</Resources>
How can i get the following:
ONE , AAA , cBackgroundTextA
ONE , BBB , cBackgroundTextB
ONE , CCC , cBackgroundTextC
TWO , DDD , cBackgroundTextD
TWO , EEE , cBackgroundTextE
...
Try something along the line:
XElement root;
foreach(var folder in root.Children()) { //For each folder
var folderType = folder.Attribute("type").Value;
foreach(var file in folder.Children()) { //For each file
Console.WriteLine($"{folderType} AND {file}");
}
}
I finally managed to get the values by:
xmlDocument = new XDocument();
xmlDocument = XDocument.Load(filePath, LoadOptions.None);
var categoryNodeList = XmlDocument.Descendants("Category");
foreach (XElement categoryItem in categoryNodeList)
{
foreach (XElement resourceItem in categoryItem.Elements())
{
var XmlObject = new ResultDetail
{
Cause = categoryItem.Attribute("Cause").Value,
Component = resourceItem.Attribute("Component").Value,
Name = resourceItem.Attribute("Name").Value
};
}
}
All required information can be gathered using single LINQ query :
var query = from cat in XmlDocument.Descendants("Category");
from res in cat.Elements()
select new ResultDetail
{
Cause = cat.Attribute("Cause").Value,
Component = res.Attribute("Component").Value,
Name = res.Attribute("Name").Value
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.