I’m parsing an xml document, that is like this:
<?xml version="1.0" encoding="UTF-8" ?>
....
<maj>true</maj>
<data>
<c>2</c>
<t>0</t>
<r>168</r>
<r>La rentrée Auchan</r>
<r>0</r>
<r>2012-08-21 00:00:00</r>
<r>2012-08-28 00:00:00</r>
<r>56</r>
<r>VL</r>
<c>2</c>
<t>1</t>
...
</data>
I want to get what there is inside the array “r”, but only the first position 1 the position 5 and 6, and only where t=0
I’ve tried to work like this, I have a listbox that bind the data :
XDocument XMLtxt = JsonConvert.DeserializeXNode(e.Result);
listClients.ItemsSource =
from c in XMLtxt.Descendants()
select new JsonB()
{
t=c.Element("t").Value.Where(x=>(int) x==0),
r1=c.Element("r").Select(..
}
the jsonB :
public class JsonB
{
public int c { get; set; }
public int t { get; set; }
public string r1 { get; set; }
public int r5 { get; set; }
public string r6 { get; set; }
public object[] r { get; set; }
}
I really need help, thank you
The XML is a bit ill-structured, so you will have to fix this first with your code.. For starters, in a simple way you can sequentially read/parse the contents so that the structure of the record is clear, and then filter them:
listof JsonB objects – name it i.e. ‘allRecords’JsonB– name it i.e. ‘lastRecord’int– name it i.e. ‘numberOfRs’<data>tagchildren, and for each child:<c>:lastRecordnumberOfRsto zerolastRecordto theallRecordslistcvalue and put it intolastRecord.c<t>:lastRecord.t<r>:numberOfRsnumberOfRsis 1, 5 or 6:lastRecord.r1or r5 or r6this way, you will have a list
allRecordswith pretty objects, and you can simply.Where(item => item.t ==0)on it.However, you may notice it to be very ‘wasteful’ if many of such objects are to be ignored. Then, you could adjust the sequential parser to filter then on-the-fly and thus behave like this
listof JsonB objects – name it i.e. ‘allRecords’JsonB– name it i.e. ‘lastRecord’int– name it i.e. ‘numberOfRs’bool– name it i.e. ‘isInteresting’, preset it to false<data>tagchildren, and for each child:<c>:isInterestingis truelastRecordto theallRecordslistlastRecordnumberOfRsto zeroadd thereset thelastRecordto theallRecordslistisInterestingto falsecvalue and put it intolastRecord.c<t>:lastRecord.tisInterestingto true<r>:numberOfRsnumberOfRsis 1, 5 or 6:lastRecord.r1or r5 or r6isInterestingis truelastRecordto theallRecordslistThis way, you will end up with a list that is already filtered and all not interesting items will be GC’ed in the meantime. Please note that the ‘isinteresting – add to list’ is done twice: once before creatig new JsonB, and then also in the final step when all children has been read. If you forget about the check-add after the loop – you may sometimes accidentially skip/ignore the last record.