c# - Check if text is already in XML file before saving -


here save method;

    private void textbox1_textchanged(object sender, eventargs e)     {         if (string.isnullorempty(txtapi.text) || string.isnullorempty(txtverc.text))         {             btapiconfirm.visible = false;         }         else         {             btapiconfirm.visible = true;         }     }      public void button1_click(object sender, eventargs e)     {          if (string.isnullorempty(txtapi.text))             {             messagebox.show("there nothing enter", "try again", messageboxbuttons.ok, messageboxicon.exclamation);             return;         }         else         {             serialization info = new serialization();             info.apikey = txtapi.text;             info.vcode = txtverc.text;             info.id = guid.newguid().tostring();             list.add(info);             serialization.savedata(list, "data.xml");         }     }      private void button2_click(object sender, eventargs e)     {         this.close();     }               private void whatisthistoolstripmenuitem_click(object sender, eventargs e)     {      }      // end of ucapin      public list<serialization> list = null;     private void ucapin_load(object sender, eventargs e)     {         list = new list<serialization>();          if (file.exists("data.xml"))          {             var doc = xdocument.load("data.xml");              foreach (xelement element in doc.descendants("serialization"))             {                 list.add(new serialization()                 {                     id = element.element("id").value,                     apikey = element.element("apikey").value,                     vcode = element.element("vcode").value                 });             }         }     } }  public class serialization {     private string id;     private string apikey;     private string vcode;      public string id     {         { return id; }         set { id = value; }     }      public string apikey     {         { return apikey; }         set { apikey = value; }     }      public string vcode     {         { return vcode; }         set { vcode = value; }      }      public static void savedata(list<serialization> list, string filename)     {         file.delete(filename);         xmlserializer sr = new xmlserializer(list.gettype());         textwriter writer = new streamwriter(filename, true);         sr.serialize(writer, list);         writer.close();     } } 

what want here check xml before saving it, duplicates.

can point me in right direction? been googleing alittle bit , can't find references want do.

copy of xml output;

<arrayofserialization> <serialization> <id>52a5900c-bdb8-4c63-93fc-10aff31b226f</id> <apikey>123</apikey> <vcode>123</vcode>     </serialization> <serialization> <id>52c85576-97ce-491b-8cdc-b213bb487d15</id> <apikey>123</apikey> <vcode>123</vcode> </serialization> </arrayofserialization> 

to compare 2 xmls should first define defines equality. order of elements, or attributes, matter?

then, convert xml's strings, because easy compare 2 strings.

you write simple method remove white space string contains xml.

as example xml: use xelement methods search both <serialization> elements, , use tostring() convert string.

xelement in using system.xml.linq;. of course use xmlelement class in system.xml, somehow xelement better.

after all, not know serialization class. parse text xml object , use xml libraries, like:

 xelement info = new xelement("arrayofserialization",     new xelement("serialization",         new xelement("id", new guid()),        new xelement("apikey", textapi.text),        new xelement("vcode", textverc.text))) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -