0

I've got this xml code:

<Turn>
    <Entry type="TurnIn" msgId="124312">
        <field tag="35" val="D"/>
        <field tag="34" val="003694"/>
        <field tag="43" val="20140916-01:29:07"/>
    </Entry>
    <Entry type="Van" msgId="234325">
        <field tag="35" val="8"/>
        <field tag="34" val="005046"/>
        <field tag="43" val="20140916-01:31:17"/>
    </Entry>
    <Entry type="fired" msgId="124864">
        <field tag="35" val="8"/>
        <field tag="34" val="005049"/>
        <field tag="43" val="20140916-01:34:49"/>
    </Entry>
    <Entry type="fired" msgId="134864">
        <field tag="35" val="8"/>
        <field tag="34" val="006324"/>
        <field tag="43" val="20140916-01:35:20"/>
    </Entry>
</Turn>    

For C#.

I need obtain val where Entry, type = "fired" and field,tag = 34 and 43 in a list. I have tried many times with the class XmlReader and Linq to XMl. I don't know how to compare one attribute with the value of the other in the same element.

isma07
  • 3
  • 3
  • don't you also need to filter on a specific date as well..? or are you wanting to return all the nodes where type = fired and field tag contains 35 AND 43...? sounds like something you can accomplish using `XPATH` – MethodMan Oct 23 '14 at 16:38
  • How do you want the values?all bundled in a collection without any reference to the parent element or each value in a collection with the reference to parent element? – terrybozzio Oct 23 '14 at 21:20
  • both answers are correct. i don't try with XPath but i think the sentence is should be correct Thanks – isma07 Oct 24 '14 at 14:51

2 Answers2

0

Use XPathDocument (see http://support.microsoft.com/kb/308333) and a XPath query like

//Turn/Entry[@type='fired']/field[@tag=34|@tag=43]@val
Alexander Haas
  • 278
  • 2
  • 11
0

This can be done using Linq to XML as below

XDocument xDoc = XDocument.Parse(yourXMLString);

//XDocument xDoc = XDocument.Load(yourXMLFilePath);

var res = xDoc.Descendants("Entry")
              .Where(x => x.Attribute("type").Value == "fired")
              .Elements("field")
              .Where(y => y.Attribute("tag").Value == "34" || y.Attribute("tag").Value == "43")
              .Select(a => a.Attribute("val").Value);
Tony Stark
  • 781
  • 6
  • 22