简体   繁体   中英

Check if text is already in XML file before saving

Here is my 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 is nothing to 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
    {
        get { return id; }
        set { id = value; }
    }

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

    public string VCODE
    {
        get { 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 i want to do here is check the XML before saving it, for duplicates.

can someone point me in the right direction? been googleing for alittle bit and can't find any good references to what i want to do.

copy of my 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 two XMLs you should first define what defines equality. Does the order of elements, or attributes, matter?

Then, convert the XML's to strings, because it is easy to compare two strings.

You could write a simple method to remove all white space from a string that contains XML.

As for your example XML: you could use the XElement methods to search for both <Serialization> elements, and use ToString() to convert to string.

XElement is in using System.Xml.Linq; . Of course you could also use the XMLElement class in System.Xml , but somehow I like the XElement better.

After all, I do not know about the Serialization class. I would parse the text into an XML object and use the XML libraries, something like:

 XElement info = new XElement("ArrayOfSerialization",
    new XElement("Serialization", 
       new XElement("ID", new GUID()),
       new XElement("APIKEY", textAPI.Text),
       new XElement("VCODE", textVerC.Text)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM