analyze xml with linq

1

I need to please, see if someone can light me up a bit, since I have an xml with information about alerts of a medication that is obtained from a REST service published on a server. the answer comes in xml format, and I'm trying to read it with linq

this is the answer I must analyze:

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Prescription Analysis</title>
  <link rel="self" type="application/atom+xml" href="/rest/api/alerts" />
  <id>/rest/api/alerts</id>
  <updated>2017-07-20T00:00:00Z</updated>
  <entry xmlns:vidal="http://api.vidal.net/-/spec/vidal-api/1.0/" vidal:categories="PRESCRIPTION_LINE">
    <title>piroxicam * 10 mg ; vía oral ; cápsula dura</title>
    <link rel="alternate" type="application/atom+xml" href="/rest/api/vmp/376" />
    <link rel="inline" href="vidal://side_effect/376/14863" />
    <link rel="inline" href="vidal://indicator/IS_NOT_COMMUNALLY_AGREED" />
    <link rel="inline" href="vidal://warning/376/2373" />
    <link rel="inline" href="vidal://side_effect/376/14873" />
    <link rel="inline" href="vidal://side_effect/376/29493" />
    <link rel="inline" href="vidal://side_effect/376/18634" />
    <category term="PRESCRIPTION_LINE" />
    <author>
      <name>VIDAL</name>
    </author>
    <id>vidal://vmp/376</id>
    <updated>2017-07-20T00:00:00Z</updated>
    <content />
    <vidal:safetyAlert>true</vidal:safetyAlert>
    <vidal:type>COMMON_NAME_GROUP</vidal:type>
    <vidal:code>376</vidal:code>
  </entry>
  <entry xmlns:vidal="http://api.vidal.net/-/spec/vidal-api/1.0/" vidal:categories="ALERT">
    <title>Contraindicación absoluta : PIROXICAM 10 mg cápsula dura</title>
    <link rel="inline" href="vidal://vmp/376" />
    <category term="ALERT" />
    <author>
      <name>VIDAL</name>
    </author>
    <id>vidal://contraindication/376/9021/1</id>
    <updated>2017-07-20T00:00:00Z</updated>
    <content type="text/html">Contraindicaci&amp;oacute;n absoluta entre PIROXICAM 10 mg c&amp;aacute;psula dura y Ni&amp;ntilde;o menor de 15 a&amp;ntilde;os</content>
    <vidal:type>CONTRA_INDICATION</vidal:type>
    <vidal:alertType name="CONTRA_INDICATION">Contraindicación</vidal:alertType>
    <vidal:severity>LEVEL_4</vidal:severity>
    <vidal:subType name="ABSOLUTE">absoluta</vidal:subType>
    <vidal:detail type="text/html" />
    <vidal:triggeredBy type="AGE" />
  </entry>
  <entry xmlns:vidal="http://api.vidal.net/-/spec/vidal-api/1.0/" vidal:categories="ALERT">
    <title>Efectos adversos: PIROXICAM 10 mg cápsula dura</title>
    <link rel="inline" href="vidal://vmp/376" />
    <category term="ALERT" />
    <author>
      <name>VIDAL</name>
    </author>
    <id>vidal://side_effect/376/39584</id>
    <updated>2017-07-20T00:00:00Z</updated>
    <content type="text/html">Insomnio</content>
    <vidal:type>SIDE_EFFECT</vidal:type>
    <vidal:alertType name="SIDE_EFFECT">Efecto adverso</vidal:alertType>
    <vidal:severity>INFO</vidal:severity>
    <vidal:detail type="text/html">Frecuencia</vidal:detail>
  </entry>
  <entry xmlns:vidal="http://api.vidal.net/-/spec/vidal-api/1.0/" vidal:categories="ALERT">
    <title>Efectos adversos: PIROXICAM 10 mg cápsula dura</title>
    <link rel="inline" href="vidal://vmp/376" />
    <category term="ALERT" />
    <author>
      <name>VIDAL</name>
    </author>
    <id>vidal://side_effect/376/14113</id>
    <updated>2017-07-20T00:00:00Z</updated>
    <content type="text/html">Edema de las extremidades inferiores</content>
    <vidal:type>SIDE_EFFECT</vidal:type>
    <vidal:alertType name="SIDE_EFFECT">Efecto adverso</vidal:alertType>
    <vidal:severity>INFO</vidal:severity>
    <vidal:detail type="text/html">Frecuencia</vidal:detail>
  </entry>
  <entry xmlns:vidal="http://api.vidal.net/-/spec/vidal-api/1.0/" vidal:categories="PATIENT">
    <title>Patient</title>
    <link rel="self" href="vidal://patient/1" title="Patient" />
    <category term="PATIENT" />
    <author>
      <name>VIDAL</name>
    </author>
    <id>vidal://patient/1</id>
    <updated>2017-09-05T16:21:09Z</updated>
    <content />
    <vidal:gender>MALE</vidal:gender>
    <vidal:height>180.0</vidal:height>
    <vidal:hepaticInsufficiency>SEVERE</vidal:hepaticInsufficiency>
    <vidal:dateOfBirth>Thu Nov 08 15:44:50 CET 2012</vidal:dateOfBirth>
    <vidal:creatin>120</vidal:creatin>
    <vidal:weight>80.0</vidal:weight>
    <vidal:breastFeeding>NONE</vidal:breastFeeding>
    <vidal:weeksOfAmenorrhea>5</vidal:weeksOfAmenorrhea>
  </entry>
</feed>

In this answer, I need to get all the nodes that contain <vidal:type>CONTRA_INDICATION</vidal:type> , or <vidal:severity>LEVEL_4</vidal:severity> .

I am using the following linq query, to get the information:

 XElement xmldoc = XElement.Parse(responseFromServer);

            XElement query = (from item in xmldoc.XPathSelectElements("severity") select item).FirstOrDefault();

but results are not delivered (the variable query = null).

    
asked by Luis Gabriel Fabres 05.09.2017 в 16:32
source

2 answers

3

What you should use is the Descendants(XName) method , and use the namespace in the search. Something like that must work for you:

XElement xmldoc = XElement.Parse(responseFromServer);
XNamespace ns = "http://api.vidal.net/-/spec/vidal-api/1.0/";
var xmlelement = xmldoc.Descendants(ns+ "type").Where(x=>x.Value== "CONTRA_INDICATION"); 

After rereading the question I understand that you want the whole node. For that, simply access the Parent of descendants that meet the criteria:

var xmlelement = xmldoc.Descendants(ns+ "type").Where(x=>x.Value== "CONTRA_INDICATION").Select(y=>y.Parent); 
    
answered by 05.09.2017 / 17:21
source
1

With a foreach that searches for each element entry and for each entry , the elements type and severity to buy them if they have the values LEVEL_4 or CONTRA_INDICATION:

            XDocument document = XDocument.Load("ruta o stream del xml");
            XElement feedElement = document.FirstNode as XElement;

 List<XElement> elementosEncontrados = new List<XElement>();
            // buscamos todos los descendientes del elemento feed
            foreach (XElement elemento in feedElement.Descendants())
            {
                // verificamos si es entry
                if (elemento.Name.LocalName == "entry")
                {
                    // como es un entry, buscamos los elementos type y severity para comprarlos con los valores
                    foreach (XElement entryData in elemento.Descendants().Where(X => X.Name.LocalName == "type" || X.Name.LocalName == "severity"))
                    {
                        // si uno de los 2 valores dan true, entonces tenemos lo que buscamos
                        if (entryData.Value == "LEVEL_4" || entryData.Value == "CONTRA_INDICATION")
                        {
                            elementosEncontrados.Add(elemento);
                            break;
                        }
                    }
                }
            }

Console.WriteLine($"Cantidad elementos encontrados: " + elementosEncontrados.Count());

// aqui analizas los elementos que tienen los valores que filtraste.
    
answered by 05.09.2017 в 17:23