简体   繁体   English

保存之前检查文本是否已经在XML文件中

[英]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. 我想在这里做的是在保存XML之前检查XML是否重复。

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; 我的XML输出的副本;

<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. 要比较两个XML,您应该首先定义定义相等性的内容。 Does the order of elements, or attributes, matter? 元素或属性的顺序重要吗?

Then, convert the XML's to strings, because it is easy to compare two strings. 然后,将XML转换为字符串,因为比较两个字符串很容易。

You could write a simple method to remove all white space from a string that contains XML. 您可以编写一个简单的方法来从包含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. 至于您的示例XML:您可以使用XElement方法搜索两个<Serialization>元素,然后使用ToString()转换为字符串。

XElement is in using System.Xml.Linq; XElementusing System.Xml.Linq; . Of course you could also use the XMLElement class in System.Xml , but somehow I like the XElement better. 当然,您也可以在System.Xml使用XMLElement类,但是以某种方式我更喜欢XElement。

After all, I do not know about the Serialization class. 毕竟,我不了解Serialization类。 I would parse the text into an XML object and use the XML libraries, something like: 我将文本解析为XML对象并使用XML库,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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