// get root achievements foreach(XElement achievement in rootCategory.Elements("achievement")) { // get root achievements }
// for each child category foreach(XElement childCategory in rootCategory.Elements("category")) { // get child category achievements foreach(XElement childAchievement in childCategory.Elements("achievement")) { // get achievement of this sub category } }
I am parsing the 'root category' and 'root achievements' okay, and I believe I am getting the child categories OK, but I am having problems getting the achievements within child categories. I think I may need to use something besides '.Elements()'? I am
very tired... ~_~
If anyone could help me out with what I'm doing wrong, or possibly a better way of doing this, I would really appreciate it.
IEnumerable<XElement> category_achievements = from p in xml.Descendants("achievement") where p.Parent.Name.Equals("category") select p;
foreach(XElement childAchievement in category_achievements)
//do your thing
...for some reason, most people assume that you can't figure out the incredibly difficult 'Mark as Answer' feature...
I on the other hand, I have faith in you collective Asp.Net forums!
Thanks for the reply. EDIT: Unfortunately, I don't think I can do something like this. I need to read the categories in document order because that's how the categories are index'd. I can't read all the achievements at once, I wouldn't know which category
they are under.
I would change the XML but it's coming from a third party site.. ie wowarmory.
Thanks for the reply. EDIT: Unfortunately, I don't think I can do something like this. I need to read the categories in document order because that's how the categories are index'd. I can't read all the achievements at once, I wouldn't know which category
they are under.
I would change the XML but it's coming from a third party site.. ie wowarmory.
/*
Using xmlDom u can do it very easily.. here i have taken ur sample xml..where
* i think u want to read the values at category/category/achievement.
* Her ein the string childAchivement..u will get the value fof it. Concated..
* or u can manipulate it the way u want it...
Ok, it turns out I had an error in my foreach loops that was causing the sub categories to parse incorrectly. The sub categories are OK now. However, I ran into another problem.
Say I have the following IEnumerable XElement called 'data', that contains:
I want to get ONLY the first 2 achievements at category/achievement. I try to use data.Elements("achievement") but it returns all 4 achievement elements. Is there any way that I can get ONLY the achievements at category/achievement?
Preferably using XDocument / XElement? I have quite a bit of code that uses
XDocument/XElement and I don't want to have to rewrite it using XmlDocument.
Thanks for the reply, Martin. I still can't figure out what I'm doing wrong =\. Here is an example of the XML I am working with:
XML
Code:
xml = new XDocument();
wc = new WebClient();
wc.QueryString.Add("r", "Icecrown");
wc.QueryString.Add("cn", "Fillwingo");
wc.QueryString.Add("categoryId", "96");
wc.Headers.Add("user-agent", "MSIE 7.0");
// note: in my code i have the 'http: in this url, but its screwing up the syntax coloring here
String url = "www.wowarmory.com/character-achievements.xml";
XmlTextReader rdr = new XmlTextReader(wc.OpenRead(url));
xml = XDocument.Load(rdr);
// get root category element, which contains achievements, and other sub categories and their achievements
IEnumerable<XElement> rootCategory = xml.Descendants("category");
// I want to get ONLY the achievements in the root category
foreach(XElement achieve in rootCategory.Elements("achievement"))
{
// .. get values
}
I keep getting all the achievements from both the root category and the achievements from within sub categories, so I'm not sure if it's a problem with the way I am getting the root category, or if it's something to do with the way i'm looping through the
elements.
pdub87
Member
6 Points
24 Posts
Reading child nodes of XDocument
Jun 18, 2009 07:51 AM|LINK
I am filling an XDocument with XML data as follows: (the parentheses are to help explain what I'm talking about)
I am having trouble parsing the "child categories" and "achievements of child categories". My code looks something like this (pseudo code):I am parsing the 'root category' and 'root achievements' okay, and I believe I am getting the child categories OK, but I am having problems getting the achievements within child categories. I think I may need to use something besides '.Elements()'? I am very tired... ~_~
If anyone could help me out with what I'm doing wrong, or possibly a better way of doing this, I would really appreciate it.
Thanks!
Pdub
xml xelement XDocument
naspinski
Participant
929 Points
171 Posts
Re: Reading child nodes of XDocument
Jun 18, 2009 08:48 AM|LINK
You could just do it all in one query:
IEnumerable<XElement> category_achievements = from p in xml.Descendants("achievement") where p.Parent.Name.Equals("category") select p; foreach(XElement childAchievement in category_achievements) //do your thingI on the other hand, I have faith in you collective Asp.Net forums!
naspinski.net
pdub87
Member
6 Points
24 Posts
Re: Reading child nodes of XDocument
Jun 18, 2009 10:16 AM|LINK
Thanks for the reply. EDIT: Unfortunately, I don't think I can do something like this. I need to read the categories in document order because that's how the categories are index'd. I can't read all the achievements at once, I wouldn't know which category they are under.
I would change the XML but it's coming from a third party site.. ie wowarmory.
kavita_khand...
Star
9767 Points
1930 Posts
Re: Reading child nodes of XDocument
Jun 18, 2009 12:02 PM|LINK
/*
Using xmlDom u can do it very easily.. here i have taken ur sample xml..where
* i think u want to read the values at category/category/achievement.
* Her ein the string childAchivement..u will get the value fof it. Concated..
* or u can manipulate it the way u want it...
*/
string str = "<category>" +
"<achievement>" +
"</achievement>" +
"<category>" +
"<achievement> (achievement of child category)" +
"</achievement>" +
"</category>" +
"</category>";
string childAchivement = "";
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(str);
XmlNodeList xNodeList = xDoc.SelectNodes("category");
foreach (XmlNode xNode in xNodeList)
{
XmlNodeList innerNodeList = xNode.ChildNodes;
foreach (XmlNode xNodeInner in innerNodeList)
{
if(xNodeInner.Name == "category")
{
childAchivement = childAchivement+ ","+xNodeInner.FirstChild.InnerText;
}
}
}
I would love to change the world, but they wont give me the source code.
pdub87
Member
6 Points
24 Posts
Re: Reading child nodes of XDocument
Jun 18, 2009 11:26 PM|LINK
Thanks, I will try that!
pdub87
Member
6 Points
24 Posts
Re: Reading child nodes of XDocument
Jun 19, 2009 05:01 AM|LINK
Ok, it turns out I had an error in my foreach loops that was causing the sub categories to parse incorrectly. The sub categories are OK now. However, I ran into another problem.
Say I have the following IEnumerable XElement called 'data', that contains:
<category> <achievement></achievement> <achievement></achievement> <category> <achievement></achievement> <achievement></achievement> </category> </category>I want to get ONLY the first 2 achievements at category/achievement. I try to use data.Elements("achievement") but it returns all 4 achievement elements. Is there any way that I can get ONLY the achievements at category/achievement?
Preferably using XDocument / XElement? I have quite a bit of code that uses XDocument/XElement and I don't want to have to rewrite it using XmlDocument.
Thanks!
Martin_Honne...
Star
14481 Points
2006 Posts
MVP
Re: Reading child nodes of XDocument
Jun 20, 2009 11:13 AM|LINK
Elements("achievement") will give you the (two) child elements of that name. Here is a complete snippet:
XElement data = XElement.Parse(@"<category> <achievement></achievement> <achievement></achievement> <category> <achievement></achievement> <achievement></achievement> </category></category>"); Console.WriteLine(data.Elements("achievement").Count());So I don't know why you would get four unless you use Descendants("achievement").
If you still have problems then show us enough code to allow us to reproduce the issue.
My blog
pdub87
Member
6 Points
24 Posts
Re: Reading child nodes of XDocument
Jun 20, 2009 09:30 PM|LINK
Thanks for the reply, Martin. I still can't figure out what I'm doing wrong =\. Here is an example of the XML I am working with: XML
Code:
xml = new XDocument(); wc = new WebClient(); wc.QueryString.Add("r", "Icecrown"); wc.QueryString.Add("cn", "Fillwingo"); wc.QueryString.Add("categoryId", "96"); wc.Headers.Add("user-agent", "MSIE 7.0"); // note: in my code i have the 'http: in this url, but its screwing up the syntax coloring here String url = "www.wowarmory.com/character-achievements.xml"; XmlTextReader rdr = new XmlTextReader(wc.OpenRead(url)); xml = XDocument.Load(rdr); // get root category element, which contains achievements, and other sub categories and their achievements IEnumerable<XElement> rootCategory = xml.Descendants("category"); // I want to get ONLY the achievements in the root category foreach(XElement achieve in rootCategory.Elements("achievement")) { // .. get values }I keep getting all the achievements from both the root category and the achievements from within sub categories, so I'm not sure if it's a problem with the way I am getting the root category, or if it's something to do with the way i'm looping through the elements.
Martin_Honne...
Star
14481 Points
2006 Posts
MVP
Re: Reading child nodes of XDocument
Jun 21, 2009 12:08 PM|LINK
The problem then is
xml.Descendants("category")
which accesses 'category' elements at all levels. You only seem to want
xml.Root.Elements("category")
to access the 'category' child elements of the Root element (the "achievements" element).
My blog
pdub87
Member
6 Points
24 Posts
Re: Reading child nodes of XDocument
Jun 21, 2009 10:06 PM|LINK