c# - How to handle temporary data and xml new attribute at initial element? -


before starting sorry awfull coding skills , hope can me advices , such.


i have hashset of unique int values:

private hashset<int> found = null; 

that use read data entity, each entry inserted on found unique , gives me whole new set of data.

so let's consider each unique int follow data:

id,m,x,y,z 

the hashset updated every second , when happens may gather repeated entities new data still have compare if not updating duplicated entity.

so made dictionary follow

dictionary<int, mylist> myitemlist = new dictionary<int, mylist>(); 

and above class mylist

public class mylist {     public int id { get; set; }     public string m { get; set; }     public string x { get; set; }     public string y { get; set; }     public string z { get; set; } } 

so here sample of found feeding dictionary:

foreach (var myfoundid in found.except(myitemlist.keys)) {    mylist listdata = new mylist();    listdata.id = get(myfoundid, "id");    listdata.m = get(myfoundid, "m");    listdata.x = get(myfoundid, "x");    listdata.y = get(myfoundid, "y");    listdata.z = get(myfoundid, "z");    myitemlist.add(myfoundid, listdata); } 

questions , doubts:

  • should change how handling above data ? should change to, make better want ?

  • when generating xml of data, need entity element have total count of objects:

    example xml:

    <?xml version="1.0" encoding="utf-8"?> <entities>     <entity id="21231" m="somedata" total="2">         <object x="somedata" y="somedata" z="somedata" />         <object x="somedata" y="somedata" z="somedata" />     </entity> </entities> 

    example code:

    writexml.writestartelement("entities"); int mylastid; foreach (keyvaluepair<int, mylist> thislist in myitemlist) {     int objectcount = 0;   
    mylist thisdata = thislist.value; if (mylastid == thisdata.id) continue; mylastid = thisdata.id;   writexml.writestartelement("entity"); writexml.writeattributestring("id", thisdata.id.tostring()); writexml.writeattributestring("m", thisdata.m);   foreach (keyvaluepair&lt;int, mylist&gt; thislistagain in myitemlist) {     mylist thisdata2 = thislistagain.value;     if (thisdata2.id == thisdata.id)     {         writexml.writestartelement("object");         writexml.writeattributestring("x", thisdata2.x);         writexml.writeattributestring("y", thisdata2.y);         writexml.writeattributestring("z", thisdata2.z);         writexml.writeendelement();         objectcount++;     }     writexml.writeattributestring("total", objectcount.tostring()); } writexml.writeendelement(); 
    } writexml.writeendelement(); writexml.close();

    the problem writexml.writeattributestring("total", objectcount.tostring()); not work in there tough element not closed yet need how sort out count before , write along entity start element.

    also think there better ways accomplish above xml generating code other 2 foreachs , guidance here.

fake data producer tests:

int myid = random.next(10000); (int = 0; < 100; i++) {     if (i % 10 == 0)         myid = random.next(10000);      mylist listdata = new mylist();     listdata.id = myid;     listdata.m = "m";     listdata.x = "x";     listdata.y = "y";     listdata.z = "z";     myitemlist.add(i, listdata); } 

you have write total before write sub-elements. but, on different note, better match intended output, class mylist should changed to...

public class mylist  {      public int id { get; set; }      public string m { get; set; }      public class myinternallist     {     public string x { get; set; }      public string y { get; set; }      public string z { get; set; }      }     public list<myinternallist> thelist = new list<myinternallist>(); }  

if find duplicates in dictionary, decide if duplicate provides new data, , add internal list. internal list provide added convenience of having total looking for.

the following example replace foreach loop on "found"...

dictionary<int, mylist> myitemlist = new dictionary<int, mylist>(); hashset<int> found = null;   // ... found gets setup  foreach (var myfoundid in found.except(myitemlist.keys)) {     mylist listdata = new mylist();     listdata.id = get(myfoundid, "id");     listdata.m = get(myfoundid, "m");      mylist.myinternallist item = new mylist.myinternallist();     item.x = get(myfoundid, "x");     item.y = get(myfoundid, "y");     item.z = get(myfoundid, "z");     listdata.thelist.add(item);      myitemlist.add(myfoundid, listdata); } 

however, basic example prevent there ever being more 1 item in internal list. xml example, assume have multiple item on list, can't tell code how might happen. review updated code , post later.

...

by read, items designed such id must unique, each id maps 1 , 1 "m", , pair can have 1 many "x","y","z" tuples. you've said collecting items , updating dictionary on 1 second interval. so, recommend create new "found" hashset each time, , not use except() extension method. in loop, create new item if dictionary not contain id, or reuse item in dictionary. so...

dictionary<int, mylist> myitemlist = new dictionary<int, mylist>(); hashset<int> found = null;   // ... setup "found"  foreach (var myfoundid in found) {     var id = get(myfoundid, "id");      mylist listdata;     if (!myitemlist.containskey(id))     {         listdata = new mylist();         myitemlist.add(id,listdata);     }     listdata = myitemlist[id];     listdata.id = get(myfoundid, id);     listdata.m = get(myfoundid, "m");     mylist.myinternallist item = new mylist.myinternallist();      item.x = get(myfoundid, "x");     item.y = get(myfoundid, "y");     item.z = get(myfoundid, "z");     listdata.thelist.add(item);     myitemlist.add(id, listdata); } 

i've made additional assumption myfoundid equal get(myfoundid,"id"), if not, switch using myfoundid everywhere use "id" (but recommend store myfoundid in mylist class somewhere since comes in handy later).


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -