user1546315
user1546315

Reputation: 703

Writing/Loading data to XML file C#

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.

Upvotes: 0

Views: 640

Answers (3)

L.B
L.B

Reputation: 116178

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;
}

Upvotes: 1

h1ghfive
h1ghfive

Reputation: 206

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.

Upvotes: 0

Felice Pollano
Felice Pollano

Reputation: 33272

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.

Upvotes: 1

Related Questions