如果存在,如何覆盖xml中的子项?

时间:2018-01-23 02:59:45

标签: c# xml

我将一些用户ID及其选择的图像添加到xml文件中。如果具有相同id的用户再次添加数据,我该如何覆盖孩子?

这就是我的xml看起来

的方式
<userimage>
   <person>
     <id>df1bd6d6c95246a1ba7adf39b66def47</id>
     <url>~/images/batman.png</url>
   </person>
   <person>
     <id>837f2ba998ec4b78b75cc39176a23626</id>
     <url>~/images/apple.png</url>
   </person>
   <person>
     <id>df1bd6d6c95246a1ba7adf39b66def47</id>
     <url>~/images/pokemon.png</url>
  </person>
</userimage>

如您所见,有两个人身份相同。如果任何记录添加了相同的ID,我希望替换该记录。

protected void AppendXml(XmlNode root, string id, string url)
    {
        XmlNode person = xdoc.CreateElement("person");
        root.AppendChild(person);
        XmlNode uname = xdoc.CreateElement("id");
        uname.InnerText = id;
        person.AppendChild(uname);
        XmlNode psswd = xdoc.CreateElement("url");
        psswd.InnerText = url;
        person.AppendChild(psswd);
    }

//code to write to xml
string id = DBConnection.getUserId(Session["User_ID"].ToString());
        string xmlpath = Server.MapPath("~/css/UserImage.xml");
        xdoc = new XmlDocument();
        if (File.Exists(xmlpath))
        {
            xdoc.Load(xmlpath);
            {
                XmlNodeList xnodelist = xdoc.GetElementsByTagName("userimage");
                foreach (XmlNode xnode in xnodelist)
                {
                    xn = xnode.LastChild.OwnerDocument.FirstChild;
                }
                AppendXml(xn, id, btnimage.ImageUrl);
                xdoc.Save(xmlpath);
            }
        }

        else
        {
            XmlDeclaration xdeclartion = xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            root = xdoc.CreateElement("userimage");
            xdoc.AppendChild(root);
            AppendXml(root,id,btnimage.ImageUrl);
            xdoc.Save(xmlpath);
        }

1 个答案:

答案 0 :(得分:1)

    var xml = "<userimage><person><id>df1bd6d6c95246a1ba7adf39b66def47</id><url>~/images/batman.png</url></person><person><id>837f2ba998ec4b78b75cc39176a23626</id><url>~/images/apple.png</url></person><person><id>df1bd6d6c95246a1ba7adf39b66def47</id><url>~/images/pokemon.png</url></person></userimage>";
    var persons = DeserializeXml<Userimage>(xml);

    var personNew = new Person() { Id = "df1bd6d6c95246a1ba7adf39b66def47", Url = "asdf" };
    var person = persons.Person.FirstOrDefault(p => p.Id == personNew.Id);
    if (person == null)
    {
        persons.Person.Add(person);
    }
    else
    {
        person.Url = personNew.Url;
    }

    var str = persons.ToString();

您可以先将XML序列化为对象数组。稍后,如果要打印出XML,则只需调用ToString()方法。

    public T DeserializeXml<T>(string str)
    {
        var se = new XmlSerializer(typeof(T));
        object obj;
        using (var reader = new StringReader(str))
        {
            obj = se.Deserialize(reader);
        }
        return (T) obj;
    }

    public static string XmlSerializeToString(object obj)
    {
        var se = new XmlSerializer(obj.GetType());
        var sb = new StringBuilder();
        using (TextWriter writer = new StringWriter(sb))
        {
            se.Serialize(writer, obj);
        }
        return sb.ToString();
    }

    [XmlRoot(ElementName = "person")]
    public class Person
    {
        [XmlElement(ElementName = "id")]
        public string Id { get; set; }
        [XmlElement(ElementName = "url")]
        public string Url { get; set; }

        public override string ToString()
        {
            return XmlSerializeToString(this);
        }
    }

    [XmlRoot(ElementName = "userimage")]
    public class Userimage
    {
        [XmlElement(ElementName = "person")]
        public List<Person> Person { get; set; }

        public override string ToString()
        {
            return XmlSerializeToString(this);
        }
    }