Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3354956
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:20:12+00:00 2026-05-18T02:20:12+00:00

Before starting I am sorry for my awfull coding skills and I hope you

  • 0

Before starting I am sorry for my awfull coding skills and I hope you can help me with advices and the such.


I have a HashSet of unique int values:

private HashSet<int> found = null;

That I use to read data from an entity, so each entry inserted on found is unique and gives me a whole new set of data.

So let’s consider that from each unique int I get the follow data:

ID,M,X,Y,Z

The HashSet is updated every second and when this happens it may gather repeated entities with new data so I still have to compare if I am not updating a duplicated entity.

So initially I made a Dictionary as follow

Dictionary<int, myList> myItemList = new Dictionary<int, myList>();

And the above Class for 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 is a sample of found feeding the 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 and doubts:

  • Should I change how I am handling the above data ? What should I change it to, to make it better for what I want ?

  • When generating the XML of that data, I need entity element to have the 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 is that writeXML.WriteAttributeString("TOTAL", objectCount.ToString()); does not work in there even tough that element is not closed yet so I need some how to sort out the count before and write it along with entity start element.

    Also I think there are better ways to accomplish the above xml generating code other then with those 2 foreachs and would like your guidance here.

Fake Data Producer for tests:

int myID = random.Next(10000);
for (int i = 0; i < 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:20:13+00:00Added an answer on May 18, 2026 at 2:20 am

    You will have to write the TOTAL before you write the sub-elements. But, on a different note, to better match your intended output, your class myList should probably be 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 you find duplicates in the Dictionary, then decide if the duplicate provides new data, and add to the internal list. The internal list provide the added convenience of having the TOTAL you are looking for.

    The following example would replace your 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, your basic example would prevent there ever being more than one item in the internal list. By the XML example, I assume that you could have multiple item on the list, but I can’t tell from your code how that might happen. I will review your updated code and post back later.

    …

    By my read, your items are designed such that ID must be unique, each ID maps to one and only one “M”, and the pair can have one to many “X”,”Y”,”Z” tuples. You’ve said you are collecting items and updating the dictionary on a one second interval. So, I would recommend that you create a new “found” hashset each time, and do not use the Except() extension method. Then in your loop, you would create a new item if your dictionary does not contain the ID, or reuse the item already in the dictionary. Like 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 the additional assumption that myFoundID is equal to get(myFoundID,”ID”), but if it is not, then you could just switch it back by using myFoundID everywhere I use “id” (but then I would recommend that you store myFoundID in your myList class somewhere since it often comes in handy later).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before starting, I'm not asking about standard coding practice or etiquette. My question is
Before starting, note that I have to update a website that is not mine,
Before starting, I do have a very particular question and if you want to
I am new to multithreaded application. I have few doubts before starting working on
For a university I was advised to learn Ruby before starting. I have a
Before Starting I want to make sure one thing whether we can make the
I was waiting for the stable release of Netbeans 6.7 before starting to use
Please note that this is not homework and i did search before starting this
As we know UML contains 13 types of diagrams (structural and behavioral) before starting
My program does some network activity in a background thread. Before starting, it pops

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.