0

I'm trying to get the values from an XElement that I'm receiving from db notification. The xml structure is like this:

<?xml version="1.0"?>
<root>
    <inserted>
        <row>
            <CODI_AVERIA>22</CODI_AVERIA>
            <NUMERO_LINIA>2</NUMERO_LINIA>
            <DIA>2016-07-17T00:00:00</DIA>
            <HORA>1899-12-30T10:26:15.790</HORA>
            <CODI_USUARI>1</CODI_USUARI>
            <ACCIO>0</ACCIO>
            <CODI_PSEUDO>-1</CODI_PSEUDO>
        </row>
    </inserted>
</root>

And this is the method that I'm using to get the data, and it returns me a List that is empty.

static void getAccio(XElement xml)
{
    try
    {
        xml.Descendants("deleted").Remove();

        var items = xml.Elements("row").Select(n => new
        {
            Codi_averia = n.Element("CODI_AVERIA").Value,
            Numero_linia = n.Element("NUMERO_LINIA)").Value,
            Accio = n.Element("ACCIO").Value
        }).ToList();
    }
    catch (Exception e)
    {
        Console.Write(e.Message);
    }
}

I have tried to get the value of each field apart and it doesn't allow me to get them separetly as XElements.

Geoff James
  • 3,122
  • 1
  • 17
  • 36
Narf92
  • 3
  • 4
  • 1
    Does your XML have a namespace? – Matias Cicero Jul 26 '16 at 13:31
  • is `` the [root element](https://en.wikipedia.org/wiki/Root_element) of your XML file or is it contained in some larger element? – dbc Jul 26 '16 at 13:33
  • @dbc it has '' element just at the begining and at the end of the xml that I copy... it's my fault – Narf92 Jul 26 '16 at 13:39
  • 1
    In that case I'd suggest you [edit] your question to create a [mcve] showing the root element and to show how you get the `XElement xml` variable. Probably the answer of @GiladGreen is correct. – dbc Jul 26 '16 at 13:42

3 Answers3

0

Use .Descendants() instead of .Elements():

var items = xml.Descendants("row").Select(n => new
    {
        Codi_averia = n.Element("CODI_AVERIA").Value,
        Numero_linia = n.Element("NUMERO_LINIA)").Value,
        Accio = n.Element("ACCIO").Value
    }).ToList();

.Elements finds only those elements that are direct descendents, i.e. immediate children. - And I assume that your XElement is the parent of the inserted section.

For difference between .Element and .Descendants see this question

If you don't want to find them in all inner elements too then do .Element("inserted").Elements("rows")

Community
  • 1
  • 1
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
0

Your document doesn't have any root element with the name row. Instead, you need to select inserted first, and enumerate the row elements of inserted:

var xml = @"<inserted>  
    <row>    
        <CODI_AVERIA>21</CODI_AVERIA>    
        <NUMERO_LINIA>2</NUMERO_LINIA>    
        <DIA>2016-07-17T00:00:00</DIA>    
        <HORA>1899-12-30T10:26:15.790</HORA>    
        <CODI_USUARI>1</CODI_USUARI>    
        <ACCIO>0</ACCIO>    
        <CODI_PSEUDO>-1</CODI_PSEUDO>  
    </row>
</inserted>";

var xdoc = XDocument.Parse(xml);

var items = xdoc.Element("inserted").Elements("row").Select(n => new
{
    Codi_averia = n.Element("CODI_AVERIA").Value,
    Numero_linia = n.Element("NUMERO_LINIA").Value,
    Accio = n.Element("ACCIO").Value

}).ToList();
Luaan
  • 62,244
  • 7
  • 97
  • 116
0

In your sample code ("NUMERO_LINIA)") is wrong because of additionnal quote and parenthesis.

This works for me :

XDocument xd = XDocument.Load("XMLFile1.xml");

var lst = (from n in xd.Descendants("row")
           select new
           {
                 Codi_averia = n.Element("CODI_AVERIA").Value,
                 Numero_linia = n.Element("NUMERO_LINIA").Value,
                 Accio = n.Element("ACCIO").Value
           }).ToList();

foreach (var item in lst)
     Console.WriteLine(string.Format("{0}{1}{2}", item.Codi_averia, item.Numero_linia, item.Accio));
mdelpeix
  • 177
  • 8