Search code examples
c#.netxmllinq-to-xmlxattribute

XDocument getting Root Attributes values


How can I get the value of title1 from this string in a datatable using xDocument

<Person ActionType = "Update"  Title1="Miss" />

I tried descendants, XAttributes and all sorts...Maybe input is wrong but

XDocument xml = XDocument.Parse(row["XMLTransaction"].ToString());

IEnumerable<XAttribute> query =
from transaction in xml.Root.Elements()
select transaction.Attribute(attribute);

Solution

  • If that string is your literal XML then you should omit the .Elements() part.

    Even shorter with XElement instead of XDocument :

     var xml = XElement.Parse(row["XMLTransaction"].ToString());
    
     IEnumerable<XAttribute> query = xml.Attributes();