1

I'm working with an xml file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<element1 xmlns="http://namespace1/">
  <element2>
    <element3>
      <element4 attr1="2009-11-09">
        <element5 attr2="NAME1">
          <element6 attr3="1">
            <element7 attr4="1" attr5="5.5" attr6="3.4"/>
          </element6>
        </element5>
        <element5 attr2="NAME2">
          <element6 attr3="1">
            <element7 attr4="3" attr5="4" attr6="4.5"/>
          </element6>
        </element5>
      </element4>
    </element3>
  </element2>
</element1>

Where I need to loop through element5 and retrieve the attributes in an Ienumberable like this:

attr1, attr2, attr3, attr4, attr5, attr6

using linq to xml and c#. I can loop through the element5 and get all the attribute2 info using but I can't figure out how to get the parent or child attributes I need.

UPDATE: Thanks for the feeback thus far. For clarity, I need to do a loop through attribute5. So basically, what I have right now (which isn't much) is . . .

XElement xel = XElement.Load(xml);
IEnumberable<XElement> cList = from el in xel.Elements(env + "element2").Element
(n2 + "element3").Elements(n2 + "element4").Elements(ns + "element5") select el;

foreach (XElement e in cList)
Console.WriteLine(e.Attribute("attr2").Value.ToString());

This will give me the value all the attr 2 in the loop but I could be going about this all wrong for what I'm trying to acheive. I also need to collect the other attributes mentioned above in a collection (the Console reference is just me playing with this right now but the end result I need is a collection). So the end results would be a collection like

attr1,      attr2, attr3, attr4, attr5, attr6
2009-11-09, name1, 1,     1,     5.5,   3.4
2009-11-09, name2, 1,     3,     4,     4.5

Make Sense?

4
  • I'm not completely sure how you want to return the data, there are multiple element5 elements. Do you want an ienumerable that returns attr1, attr2, attr3, .., attr6, attr1 (of next element)? Or an enumerable of objects with attr1 till attr6? Commented Dec 11, 2009 at 23:17
  • Would be nice to get a little feedback on whether or not we've answsered your question, and if not, what we don't have quite right... You've got two answers now, please give us some feedback... Commented Dec 11, 2009 at 23:55
  • tried adding a comment but ran past the 600 character limit so I updated the post. Commented Dec 12, 2009 at 1:22
  • Updating the post was the best thing to do. Welcome to StackOverflow! Commented Dec 13, 2009 at 21:55

3 Answers 3

1

Use linq-to-xml to navigate the tree up (parent/ancestors) or down (element/elements/descendants). See msdn for details.

XDocument doc

var q = from element5 in doc.Elements("element5")
        let element4 = element5.Parent
        let element6 = element5.Element("element6")
        let element7 = element6.Element("element7")
        select new {
                     attr1 = (DateTime)element4.Attribute("attr1"),
                     attr2 = (string)element5.Attribute("attr2"),
                     attr3 = (int)element6.Attribute("attr3"),
                     attr4 = (int)element7.Attribute("attr4")
                     attr5 = (float)element7.Attribute("attr5")
                     attr6 = (float)element7.Attribute("attr6")
                   }
Sign up to request clarification or add additional context in comments.

Comments

1
xdoc = XDocument.Load(Server.MapPath("Temp.xml"))
Dim x = From el As XElement In xdoc...<vehicles>.Descendants.Where(Function(f) [email protected] = id.ToString)
Dim at = From a In x.Attributes()
For Each t In at
Dim n = t.Name
Dim v = t.Value
ProcessForm(n.ToString, v)
Next

1 Comment

Sorry this is in VB but you can easily convert to c#. If you need to get all the attributes of an element without knowing the name or the value, use this to iterate through all the attributes of an element and do whatever. Ignore my processform() as this is the function I use to handle the attributes. Hope this helps
0

Not perfectly clear, but this might be a starting point:

XElement el = // something here
el.Descendants().Concat(new XElement[]{el}).SelectMany(e => e.Attributes())

I don't think I have exactly what you're looking for... You make it sound like you are starting with a reference to an element5 and you want to go up and down the tree?

EDIT: I think this might be what you're looking for (after reading your question yet again):

XElement el = // something here
el.Descendants().Concat(new XElement[]{el}).Where(e => e.Name.LocalName == "element5").SelectMany(e => e.Attributes())

1 Comment

Sorry for the lack of clarity, I added some addtional notes above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.