1

I tried to get the attributes from XNode I use the following code

        XDocument document = XDocument.Load(FilePath);

        var Elements = from p in document.Descendants(modality) select p.Elements("Key_Part");

        //var Attriputess = from p in document.Descendants(modality) select p.Attributes();    
        foreach (var Element in Elements)
        {

            foreach (var node in Element.Nodes())
            {

                XmlReader reader = node.CreateReader();



                string a = reader.GetAttribute("Type"); 


            }



        }

a always equal null the XML type like this

<ShortcutList Version="8">
  <Doctor>
    <Key_Part >
        <Key1 Name = "XX" Type= "XXXXXXXXX" > rrrr</Key1>
        <Key2 Name = "XasfsaX" Type= "XXXXsafasfXXXXX" > rrsfsfrr</Key1>
    </Key_Part>

I want to get Key1 , Key2 attributes and value

AMH
  • 6,363
  • 27
  • 84
  • 135

2 Answers2

1

I must say I've fixed XML you've provided before got working query below. So you've not closed two last tags and you've closed Key2 by </Key1> tag what is wrong XML.

Try out following, this will return list of entries (one per key) where each entry of anonymous type with properties: Name, Value, Type

var keys = xdoc.Descendants("ShortcutList")
               .Descendants("Doctor")
               .Descendants("Key_Part")
               .DescendantNodes()
               .OfType<XElement>()
               .Where(d => d.Name.LocalName.Contains("Key"))
               .Select(e => new {
                                 Value = e.Value, 
                                 Name = e.Attribute("Name").Value, 
                                 Type = e.Attribute("Type").Value
                                })
              .ToList();

EDIT: Fixed XML

<ShortcutList Version="8">
  <Doctor>
    <Key_Part >
        <Key1 Name = "XX" Type= "XXXXXXXXX" > rrrr</Key1>
        <Key2 Name = "XasfsaX" Type= "XXXXsafasfXXXXX" > rrsfsfrr</Key2>
    </Key_Part>
</Doctor>
</ShortcutList>
sll
  • 61,540
  • 22
  • 104
  • 156
0

You need to make sure that the reader is on the element before you can start getting attributes. However, unless you have a good reason to do so, you shouldn't need an XmlReader at all to get attribute values.

Edited to add the requested example:

foreach (var a in document.Descendants(modality).Elements("Key_Part").Select(e => e.Attribute("Type").Value)) {
    // the variable a is a string with the attribute value
}

Or like this:

foreach (var attr in document.Descendants(modality).Elements("Key_Part").Attributes("Type")) {
    string a = attr.Value;
}
Lucero
  • 59,176
  • 9
  • 122
  • 152
  • So how to get the attributes without that , give example plesae – AMH Dec 11 '11 at 16:09
  • By using the [XElement.Attribute()](http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.attribute.aspx) method for instance. – Lucero Dec 11 '11 at 16:12
  • am sorry but could You give example using the xml, please provide code , there's no Element.Attribute – AMH Dec 11 '11 at 16:16
  • but I need to fill datatable with the values and attribute, this will cause performance issue , – AMH Dec 11 '11 at 16:28
  • Be specific please. "It doesn't work" is not specific, and what that with the datatable? – Lucero Dec 11 '11 at 16:32