Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys/Gals,

struggeling a little here (this is all still really new to me so baby language);

I want to check for duplicates and if I try and enter in the same value a message box comes up telling me, I want to check APIKEY and VCODE. If they match values already in my XML I don't want it to add again.

XML
<?xml version="1.0" encoding="utf-8"?>
<APISAVE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>3e9316bc-6190-4d1d-81e1-9130755ee0ad</ID>
  <APIKEY>1234</APIKEY>
  <VCODE>1234</VCODE>
</APISAVE><?xml version="1.0" encoding="utf-8"?>
<APISAVE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>c67ade2a-d568-40d2-a72f-29f896903fd1</ID>
  <APIKEY>1234567</APIKEY>
  <VCODE>1234567</VCODE>
</APISAVE><?xml version="1.0" encoding="utf-8"?>
<APISAVE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>12e777f2-910e-443f-ab6d-b1d2053ae12b</ID>
  <APIKEY>1234</APIKEY>
  <VCODE>1234</VCODE>
</APISAVE>


My Code looks like this

APISAVE.cs:

C#
public class APISAVE
    {
        static void main(string[] args)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("data.XML");
            XmlNodeList userNodes = xmldoc.SelectNodes("data.XML");
            foreach (XmlNode userNode in userNodes) ;

        }

        private string id;
        private string APIkey;
        private string VCode;

        public string ID
        {
             get { return id; }
             set { id = Guid.NewGuid().ToString(); }
        }

        public string APIKEY
        {
            get { return APIkey; }
            set { APIkey = value; }
        }

        public string VCODE
        {
            get { return VCode; }
            set { VCode = value; }

        }

        public static void SaveData(object obj, string Filename)
        {

            XmlSerializer sr = new XmlSerializer(obj.GetType());
            TextWriter writer = new StreamWriter(Filename, true);
            sr.Serialize(writer, obj);
            writer.Close();

        }
    }


Here is my Button:

C#
private void button1_Click(object sender, EventArgs e)
{
    {
        try
        {


            APISAVE info = new APISAVE();
            info.APIKEY = txtAPI.Text;
            info.VCODE = txtVerC.Text;
            info.ID = info.ID;
            APISAVE.SaveData(info, "data.XML");

        }

        catch (Exception ex)

        {
            MessageBox.Show(ex.Message);
        }
    }
}


my end goal is to use the data put in here later on.
Posted
Updated 26-Aug-15 6:08am
v7
Comments
Andy Lanng 26-Aug-15 9:55am    
does the id have to be a sequential int? if not you could use a GUID which is always unique
Member 11841936 26-Aug-15 10:39am    
no it doesn't I'm just trying to add the ID number because I want to use the data later and if there are multiple entries I want to be able to pick.
Andy Lanng 26-Aug-15 10:43am    
Ok - just use a GUID: Guid.NewGuid().ToString() for example
Member 11841936 26-Aug-15 11:03am    
this works well, random huge number.. on to part 2, what if I want to scan the XML & if the ID is different return it and add the Vcode & APIKEY into a weblink?
Andy Lanng 26-Aug-15 11:10am    
Not sure I understand what you need to do. It might be a good idea to use the "Improve Question" button and add part deux in more detail.

1 solution

Why dealing with XML by yourself if you can use serialization instead? Please see:
https://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900