Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何反序列化XML文件,然后再次序列化,并向该文件添加新项?_C#_Asp.net_Xml Serialization - Fatal编程技术网

C# 如何反序列化XML文件,然后再次序列化,并向该文件添加新项?

C# 如何反序列化XML文件,然后再次序列化,并向该文件添加新项?,c#,asp.net,xml-serialization,C#,Asp.net,Xml Serialization,我需要将xml文件反序列化为列表或数组,然后在1上放大此列表或数组,然后再次序列化文件并将新对象添加到此xml文件 我写了一些类似的东西,但它不起作用: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.IO;

我需要将xml文件反序列化为列表或数组,然后在1上放大此列表或数组,然后再次序列化文件并将新对象添加到此xml文件

我写了一些类似的东西,但它不起作用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void deserialize()
    {
        string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
        using (FileStream fs = new FileStream(path, FileMode.Open))
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
            List<Person> persons = (List<Person>)ser.Deserialize(fs);
            fs.Close();
            //List<Person> persons1 = ((List<Person>)os.Length + 1);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
        if (File.Exists(path))
        {
            deserialize();
        }
        else
        {
            List<Person> o = new List<Person>(TextBox1.Text, TextBox2.Text, int.Parse(TextBox3.Text));
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
                ser.Serialize(fs, o);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Xml;
使用System.IO;
使用System.Xml.Serialization;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
public void反序列化()
{
字符串路径=Server.MapPath(Request.ApplicationPath+“/test.xml”);
使用(FileStream fs=newfilestream(路径,FileMode.Open))
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
列表人员=(列表)序列反序列化(fs);
fs.Close();
//List persons1=((List)os.Length+1);
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
字符串路径=Server.MapPath(Request.ApplicationPath+“/test.xml”);
if(File.Exists(path))
{
反序列化();
}
其他的
{
List o=新列表(TextBox1.Text、TextBox2.Text、int.Parse(TextBox3.Text));
使用(FileStream fs=newfilestream(路径,FileMode.Create))
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
序列序列化(fs,o);
}
}
}
}

感谢您的帮助:)

您的反序列化代码实际上是禁止操作的-您不会返回任何内容。将其更改为返回反序列化列表,如下所示:

private List<Person> Deserialize(string path)
{
    using (FileStream fs = new FileStream(path, FileMode.Open))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        return (List<Person>) ser.Deserialize(fs);//There is an error in XML document (2, 2). this error i got here
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
    List<Person> people = File.Exists(path) ? Deserialize(path)
                                            : new List<Person>();
    people.Add(new Person(TextBox1.Text, TextBox2.Text,
                          int.Parse(TextBox3.Text));
    using (FileStream fs = File.OpenWrite(path))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        ser.Serialize(fs, o);
    }
}
私有列表反序列化(字符串路径)
{
使用(FileStream fs=newfilestream(路径,FileMode.Open))
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
return(List)ser.Deserialize(fs);//XML文档(2,2)中有一个错误。我在这里得到了这个错误
}
}
然后在click事件中,类似以下内容:

private List<Person> Deserialize(string path)
{
    using (FileStream fs = new FileStream(path, FileMode.Open))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        return (List<Person>) ser.Deserialize(fs);//There is an error in XML document (2, 2). this error i got here
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
    List<Person> people = File.Exists(path) ? Deserialize(path)
                                            : new List<Person>();
    people.Add(new Person(TextBox1.Text, TextBox2.Text,
                          int.Parse(TextBox3.Text));
    using (FileStream fs = File.OpenWrite(path))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        ser.Serialize(fs, o);
    }
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
字符串路径=Server.MapPath(Request.ApplicationPath+“/test.xml”);
List people=File.Exists(路径)?反序列化(路径)
:新列表();
添加(新人物)(TextBox1.Text、TextBox2.Text、,
int.Parse(TextBox3.Text));
使用(FileStream fs=File.OpenWrite(path))
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
序列序列化(fs,o);
}
}

您的反序列化代码实际上是一个no op-您没有返回任何内容。将其更改为返回反序列化列表,如下所示:

private List<Person> Deserialize(string path)
{
    using (FileStream fs = new FileStream(path, FileMode.Open))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        return (List<Person>) ser.Deserialize(fs);//There is an error in XML document (2, 2). this error i got here
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
    List<Person> people = File.Exists(path) ? Deserialize(path)
                                            : new List<Person>();
    people.Add(new Person(TextBox1.Text, TextBox2.Text,
                          int.Parse(TextBox3.Text));
    using (FileStream fs = File.OpenWrite(path))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        ser.Serialize(fs, o);
    }
}
私有列表反序列化(字符串路径)
{
使用(FileStream fs=newfilestream(路径,FileMode.Open))
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
return(List)ser.Deserialize(fs);//XML文档(2,2)中有一个错误。我在这里得到了这个错误
}
}
然后在click事件中,类似以下内容:

private List<Person> Deserialize(string path)
{
    using (FileStream fs = new FileStream(path, FileMode.Open))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        return (List<Person>) ser.Deserialize(fs);//There is an error in XML document (2, 2). this error i got here
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
    List<Person> people = File.Exists(path) ? Deserialize(path)
                                            : new List<Person>();
    people.Add(new Person(TextBox1.Text, TextBox2.Text,
                          int.Parse(TextBox3.Text));
    using (FileStream fs = File.OpenWrite(path))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        ser.Serialize(fs, o);
    }
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
字符串路径=Server.MapPath(Request.ApplicationPath+“/test.xml”);
List people=File.Exists(路径)?反序列化(路径)
:新列表();
添加(新人物)(TextBox1.Text、TextBox2.Text、,
int.Parse(TextBox3.Text));
使用(FileStream fs=File.OpenWrite(path))
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
序列序列化(fs,o);
}
}

为什么不将新对象序列化,然后操纵XML以插入新对象XML中的其他元素?我没有发明这个解决方案,但如果你的解决方案很简单,那么我可以用你的方式:)我想说的是,虽然我这次能够回答这个问题,但今后请给出比“它不起作用”更详细的信息阅读有关如何编写好问题的提示。为什么不将新对象序列化,然后操纵XML以从新对象的XML中插入其他元素?我并没有发明这个解决方案,但如果您的解决方案很简单,那么我可以用您的方法:)我想请注意,虽然我这次能够回答这个问题,但今后请给出比“它不起作用”更详细的细节。阅读以获得如何写好问题的提示。@harry180:我真的不明白你的意思,我很抱歉。@harry180:我真的不明白你的意思,我很抱歉。