序列化XML并将新元素附加到XML文件 [英] Serialize XML and append new elements to XML file

查看:87
本文介绍了序列化XML并将新元素附加到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用XML模式中的XSD2Code创建了XML类。该类具有方法SaveToFile。因此,当我尝试添加新元素并将其保存到XML文件时,它会覆盖整个内容。

I have created a XML class using XSD2Code from my XML Schema. The class having a method SaveToFile. So when I am trying to add new elements and save it to the XML file it overwrites the entire content.

是否有人知道如何插入元素(附加到XML) )通过序列化到XML文件。

Does anyone have idea how to insert elements(append to the XML) to the XML file via Serialization.

例如:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <element>content1</element>
</root>

如果这是XML文件,我需要添加一个元素,结果应如下所示,使用

If this is the XML file I need to add an element and the resulting should come as like below using Serialization.

<?xml version="1.0" encoding="utf-8"?>
<root>
  <element>content1</element>
  <element>content2</element>
</root>


推荐答案

您的请求似乎可以通过添加其他项来解决进入您的列表集合。

Your request would seem to be resolvable by adding another item into your List collection.

MyTypeFromXmlSchema myType = new MyTypeFromXmlSchema();
myType.MyElementItems = new List<MyElementItem>();
myType.MyElementItems.Add { new MyElementItem { value = "value1" } };
myType.MyElementItems.Add { new MyElementItem { value = "value2" } };

然后使用XmlSerializer进行序列化类型。

Then serialize type using XmlSerializer.

这篇关于序列化XML并将新元素附加到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆