0

I have an application in which a user will be saving information that will later be pulled up. Each 'Trial' will have its own XML file and within that XML file there will be specific events that are identified by an EventID and specific dogs competing in each event that will have their own DogID.

I need to find a way to check whether or not a specifric event or dog within a specific event has already been written to this file. Below is the code that will create this XML file for the very first time and then be updated when creates a specific event and dog for this file.

XmlWriter w = XmlWriter.Create(fs);

            w.WriteStartDocument();
            w.WriteStartElement("Event");
            for (int counter = 0; counter < registeredEventCount; counter++)
            {

                string eventString = registeredArrayList[counter].ToString();
                // eventString = eventString.Replace(" ", "");

                w.WriteStartElement("Event");
                w.WriteAttributeString("id", eventString);
                // Write a product.
                w.WriteStartElement("dogId");
                w.WriteElementString("eventID", eventSelectComboBox.SelectedItem.ToString());
                w.WriteElementString("ukcNumber", ukcDogNumTextBox.Text.ToString());
                w.WriteElementString("breed", breedTextBox.Text.ToString());
                w.WriteElementString("dogName", dogNameTextBox.Text.ToString());
                w.WriteEndElement();
                w.WriteEndElement();
            }

            w.WriteEndDocument();
            w.Flush();

Any suggestions? I am having trouble finding an effient way to check whether an event has already been created and a dog has already been created wihtin an event.

3
  • which version of .Net are you using? Commented Aug 7, 2012 at 12:25
  • Can you keep a list of events/dogs during runtime? Then you only parse your XML file when you load your application and perform all further operations on a List<Dog> or List<Event>. An whenever you change your data you write the changes back to the XML file. Commented Aug 7, 2012 at 12:43
  • Show some xml and which event are you checking for? Commented Aug 7, 2012 at 16:46

3 Answers 3

1

Use a Dictionary or an HashedSet populated during the file writing, check that dictionary for the proper key in order to check if the dog / event is already "seen" and to skip it.

Sign up to request clarification or add additional context in comments.

10 Comments

I haven never used Dictionary or a HashedSet. Could you provide any examples for me? I wouldn't know where to start with that suggestion.
@user1546315 start from here: dotnetperls.com/dictionary but you find a lot of code if you google for c# Dictionary
@Jan Dictionary<> or Hashedset<> allow a fast lookup
will the dictionary values remain after the program is closed and re-open. I need these values to always be there unless the user wants to delete the records.
@Jan yes in general, not if there is few elements: dotnetperls.com/dictionary-time
|
1

using XmlSerializer could be easier, For example

XmlSerializer serializer = new XmlSerializer(typeof(Trial));
serializer.Serialize(stream, trialObject);

public class Trial
{
    public List<Event> Events;
}

public class Event
{
    public string eventID;
    public string dogId;
    public string ukcNumber;
    public string breead;
    public string dogName;
}

1 Comment

+1 for showing how to use a List<Event> object. This is surprisingly elusive to find documentation for and used to cause me a great deal of grief trying to figure out properly. @user1546315 : Pay attention to this code, it is the method I use for dealing with XML.
0

I think you need to take a look at LINQ to XML : http://msdn.microsoft.com/fr-fr/library/bb387098.aspx

This way you can query your XML document and retrieve what you need to check.

Side note : I recommend you to use the keyword 'using' to automtically Dispose() your streamWriter. Check this http://msdn.microsoft.com/fr-fr/library/yh598w02%28v=vs.80%29

You'll learn that basically, every class that implement IDisposable can be used with the using intruction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.