0

I'm trying to use XElement to find out the parent node of a child. See the example below. This is my data:

<Data>
    <Description>King</Description>
    <Data>
        <Description>Mike</Description>
        <Data id="GUID1" Description="THIS IS A TEST" />
    </Data>
    <Data>
        <Description>David</Description>
        <Data id="GUID2" Description="THIS IS A TEST" />
        <Data id="GUID3" Description="THIS IS A TEST" />
    </Data>
    <Data id="GUID4" Description="THIS IS A TEST" />
</Data>

So the Parent(King) has two childs Mike and David. What I want is to find out who is the Parent of "David" How can I find this out?

Here is my code below:

protected void Page_Load(object sender, EventArgs e)
{
    var s =
     @"<Data>
           <Description>King</Description>
           <Data>
             <Description>Mike</Description>
             <Data id=""GUID1"" Description=""THIS IS A TEST"" />
           </Data>
           <Data>
             <Description>David</Description>
             <Data id=""GUID2"" Description=""THIS IS A TEST"" />
             <Data id=""GUID3"" Description=""THIS IS A TEST"" />
           </Data>
           <Data id=""GUID4"" Description=""THIS IS A TEST"" />
       </Data>";

    var doc = XElement.Load(new StringReader(s));

    var result = (from data in doc.Descendants("Data")
                  where (string)data.Attribute("id") != null
                  select new
                  {
                      Id = data.Attribute("id").Value,
                      Decription = data.Attribute("Description").Value,
                      Child = data.Parent.Element("Description").Value,
                      Parent = data.Parent.Parent.Value **** THIS LINE ****
                  });

    foreach (var element in result)
    {
        Console.WriteLine("{0} {1}", element.Id, element.Decription);
    }
}

I tried everything but no luck. I keep getting the whole parent results for data.Parent.Parent.Value ="KingMikeDavid" which is wrong. How do I return just "King" without its children?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user929153
  • 475
  • 2
  • 11
  • 25

2 Answers2

3

data.Parent.Parent refers to the entire Data element, and its value is the combined value of its descendants. You need the value of its Description child:

Parent = data.Parent.Parent == null ? null :
    data.Parent.Parent.Element("Description").Value
user1096188
  • 1,809
  • 12
  • 11
  • sorry i forgot to add another line in the xml please see updated version. This throws an null execption?? – user929153 Feb 27 '12 at 13:36
  • But this element is a child of the topmost data element, it 'belongs' to the king, so to say, which doesn't have any parents.... I edited the answer for this case. – user1096188 Feb 27 '12 at 13:45
0

I'm not sure of LINQ but if it doesn't work you can use XPATH to do this. http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C Specially see the expression table in middle. You need something like this expression.

/catalog/cd[last()]   //selects the last cd child of catalog
Mujtaba Hassan
  • 2,495
  • 2
  • 20
  • 29