2011-05-08 149 views
1

我必須先反序列化XML文件名單,然後擴大這個名單,並與更多的(需要對這個倍數)加1對象從XML問題從XML文件序列化/反序列化對象/

代碼序列化所有:從的.cs

<?xml version="1.0"?> 
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
      <name>Danie</name> 
      <lastName>McJackie</lastName> 
      <age>27</age> 
</Person> 

代碼文件:我評論在那裏我得到錯誤和複製錯誤消息

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) 
    { 

    } 
    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). 
      // I got this error and don't know how to manage with it. 
     } 
    } 

    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, people); 
     } 


    } 
} 

和至少類連載:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 



[Serializable] 
public class Person 
{ 

    public string name; 
    public string lastName; 
    public int age; 
    public Person(string _name, string _lastName, int _age) 
    { 
     name = _name; 
     lastName = _lastName; 
     age = _age; 
    } 
    public Person() 
    { 

    } 
} 

異常詳細信息:

System.InvalidOperationException was unhandled by user code 
Message=There is an error in XML document (2, 2). 
Source=System.Xml 
StackTrace: 
     at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,            String encodingStyle, XmlDeserializationEvents events) 
    at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream) 
    at _Default.Deserialize(String path) in e:\Programy c#\WebSites\ASP8\Default.aspx.cs:line 22 
    at _Default.Button1_Click(Object sender, EventArgs e) in e:\Programy c#\WebSites\ASP8\Default.aspx.cs:line 29 
    at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
    at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
     at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)     
     at System.Web.UI.Page.ProcessRequestMain(Boolean                includeStagesBeforeAsyncPoint,  Boolean includeStagesAfterAsyncPoint) 
    InnerException: System.InvalidOperationException 
     Message=<Person xmlns=''> was not expected. 
    Source=_1r-cm1p 
    StackTrace: 
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read3_ArrayOfPerson() 
    InnerException: 

這是我添加了最後一次編輯

反序列化文件的.cs:

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> os = (List<Person>)ser.Deserialize(fs); 
     foreach (var i in os) 
     { 
      Label1.Text += i.Name + "<hr />" + i.LastName + "<hr />" + i.Age.ToString() + "<hr />"; 
     } 

     fs.Close(); 

一切工作很好,謝謝大家的幫助: )

+0

什麼錯誤?異常詳情? – Oded 2011-05-08 19:53:43

+1

只是一個幫手 - 輕輕改變你的命名約定_name應該是你的私有成員,並且你的公共字符串名應該是公共字符串Name(note大寫) – 2011-05-08 19:54:25

+1

2,2的錯誤幾乎總是意味着在文件開始處的回車 - 這是怎麼回事? – 2011-05-08 19:57:13

回答

3

而不是

XmlSerializer ser = new XmlSerializer(typeof(List<Person>)); 

嘗試

XmlSerializer ser = new XmlSerializer(typeof(Person)); 
+0

當我改變我得到錯誤: 無法轉換'Person'類型的對象來鍵入'System.Collections.Generic.List'1 [Person]'。 – harry180 2011-05-08 20:30:17

+0

但它現在正在工作。只需調整你的代碼,因爲它返回一個Person。 – ariel 2011-05-08 20:32:23

+0

對不起,我看到你在序列化Person列表,所以你的XML應該有多個人,對吧? – ariel 2011-05-08 20:35:33

0

有沒有在您的命名空間符行結束?

<?xml version="1.0"?> 
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:// 
www.w3.org/2001/XMLSchema"> 

應該

<?xml version="1.0"?> 
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

當從問題複製,線路末端似乎有

+0

你是什麼意思?但我不明白 – harry180 2011-05-08 20:46:31

+1

我希望你能看到兩個片段的區別嗎? – sehe 2011-05-08 21:28:39

+0

不,我沒有看到不同的一切對我來說是一樣:( – harry180 2011-05-09 08:44:44