2011-09-07 102 views
2

我想使用XML序列化逐个添加多个记录。我有三个文本框和一个按钮,用于从用户获取数据,然后使用XML文件中的serializae。但是,当我自己添加一条记录时很好,但对于另一条记录,它声明了多个根元素,我无法处理这些元素。XML序列化

我做的XML序列化和我得到的XML文件

**<?xml version="1.0" encoding="utf-8"?> 
<sroot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:my-examples:shaping">** 
    <CustomerId>3a8bb49e-f616-49a5-8ec8-1886881c3042</CustomerId> 
    <FirstName>HASEEB</FirstName> 
    <LastName>KHAN</LastName> 
    <CustomerEmail>SDCFDS</CustomerEmail> 
**</sroot><?xml version="1.0" encoding="utf-8"?> 
<sroot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:my-examples:shaping">** 
    <CustomerId>6d2cbe5e-e1fb-4526-9f98-bf396b4ded55</CustomerId> 
    <FirstName>ammae</FirstName> 
    <LastName>wdfjk</LastName> 
    <CustomerEmail>sdkcbnk</CustomerEmail> 
</sroot> 

这个错误正如你可以看到上面的XML代码,有多个根元素是写,我不能够解决,我有一类称为customer.cs和代码写在这个类是

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

namespace XmlSearlizeProject.Classes 
{ 
    [Serializable] 
    [XmlRoot(ElementName = "sroot", Namespace = "urn:my-examples:shaping")] 
    public class Customer 
    { 
     string id = default(string); 
     string firstName = default(string); 
     string lastName = default(string); 
     string email = default(string); 
     public Customer() 
     { 

     } 
     public Customer(string id, string firstName, string lastName, string email) 
     { 
      CustomerId = id; 
      FirstName = firstName; 
      LastName = lastName; 
      Email = email; 

     } 
     [XmlElement("CustomerId")] 
     public string CustomerId 
     { 
      get 
      { 
       return this.id; 
      } 
      set 
      { 
       this.id = value; 
      } 
     } 
     [XmlElement("FirstName")] 
     public string FirstName 
     { 
      get 
      { 
       return this.firstName; 
      } 
      set 
      { 
       this.firstName = value; 
      } 
     } 
     [XmlElement("LastName")] 
     public string LastName 
     { 
      get 
      { 
       return this.lastName; 
      } 
      set 
      { 
       this.lastName = value; 
      } 
     } 
     [XmlElement("CustomerEmail")] 
     public string Email 
     { 
      get 
      { 
       return this.email; 
      } 
      set 
      { 
       this.email = value; 
      } 
     } 
    } 
} 

而我的C#代码是

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Xml.Serialization; 
using System.IO; 
using System.Xml; 
using System.Text; 
namespace XmlSearlizeProject.WebPages 
{ 
    public partial class CustomerPage : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     private void GeneralFunction(Stream xmlStream) 
     { 
      //xmlStream.Close(); 
      string customerId = Guid.NewGuid().ToString(); 
      Classes.Customer customer = new Classes.Customer(customerId,this.FirstNameTextBox.Text,this.LastNameTextBox.Text,this.EmailTextBox.Text); 
      XmlSerializer xmlSerializer = new XmlSerializer(typeof(Classes.Customer)); 
      XmlDocument document = new XmlDocument(); 
      document.Load(xmlStream); 

      XmlElement id = document.CreateElement("Id"); 
      id.InnerText = customerId; 
      document.DocumentElement.InsertAfter(id, document.DocumentElement.LastChild); 

      XmlElement firstName = document.CreateElement("rtgr"); 
      firstName.InnerText = customer.FirstName; 
      document.DocumentElement.InsertAfter(firstName, document.DocumentElement.LastChild); 

      XmlElement lastName = document.CreateElement("rtgtr"); 
      lastName.InnerText = customer.LastName; 
      document.DocumentElement.InsertAfter(lastName, document.DocumentElement.LastChild); 

      XmlElement email = document.CreateElement("grbfr"); 
      email.InnerText = customer.Email; 
      document.DocumentElement.InsertAfter(email, document.DocumentElement.LastChild); 
      XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlStream, Encoding.UTF8); 
      xmlSerializer.Serialize(xmlTextWriter, customer); 

      xmlStream.Close(); 
     } 

     private void SerializeCustomer() 
     { 
      if (File.Exists(Server.MapPath("~/Customer.xml"))) 
      { 
       Stream xmlWriterStream = new FileStream(Server.MapPath("~/Customer.xml"), FileMode.Open, FileAccess.ReadWrite); 
       GeneralFunction(xmlWriterStream); 
       xmlWriterStream.Close(); 
      } 
     } 

     private void DeSerializeCustomer() 
     { 
      Stream xmlReaderStream = new FileStream(Server.MapPath("~/Customer.xml"), FileMode.Open, FileAccess.Read, FileShare.Read); 
      XmlSerializer xmlDeSerializer = new XmlSerializer(typeof(Classes.Customer)); 
      Classes.Customer customer = (Classes.Customer)xmlDeSerializer.Deserialize(xmlReaderStream); 
     } 

     protected void SubmitButton_Click(object sender, EventArgs e) 
     { 
      //if (!File.Exists(Server.MapPath("~/Customer.xml"))) 
      //{ 
       SerializeCustomer(); 
      //} 
      //else 
      //{ 
      // DeSerializeCustomer(); 
      //} 
     } 
    } 
} 

回答

2

它看起来像是一次序列化一个客户,而不是将客户的列表/数组/客户序列化到XML文件。

序列化一个可行,因为你有1个根元素 - 客户。但是,在序列化很多时,您需要将客户集合序列化为XML文件。

,那么你就会有(只是为了举例说明):

<Customers> 
<sroot/> 
<sroot/> 
</Customers> 

几篇文章来看看这个:

C# Serializing a Collection of Objects

http://wcode.net/2009/08/serialize-collection-of-object-in-c/

http://www.codeproject.com/KB/cs/objserial.aspx

0

定义为chema定义(xsd)的格式,你希望你的xml看起来像。然后你可以使用一些外部工具,如xsd2code。这会自动生成您的“客户”类到您的xsd的格式。 (如果你正在手动做)。然后你的xml将会和你在xsd中定义的方式一致。试一试,为你的xml定义一个xsd总是一个更好的习惯。

+0

但我的问题是从xml文件中删除多个根元素。有没有其他方法可以从xml文件中删除多个根元素,而无需使用xsd? – haseebkhan

0

你可以只从列表像这样继承...

[Serializable] 
[XmlRoot(ElementName = "sroot", Namespace = "urn:my-examples:shaping")] 
public class CustomerList : List<Customer> 
{ 
} 

[Serializable] 
public class Customer 
{ 
    ... 
} 

用法示例...

CustomerList customerList = new CustomerList 
{ 
    new Customer 
    { 
     FirstName = "John", 
     LastName = "Doe", 
     Email = "[email protected]", 
     CustomerId = "123" 
    }, 
    new Customer 
    { 
     FirstName = "Jane", 
     LastName = "Doe", 
     Email = "[email protected]", 
     CustomerId = "456" 
    } 
}; 

然后你的方法将序列列表(CustomerList的实例)...

SerializeCustomerList(CustomerList customers) 
{ 
    // Do serialize CustomerList instance... 
} 

然后将生成的XML会是什么样子......

<sroot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:my-examples:shaping"> 
    <Customer> 
    <CustomerId>123</CustomerId> 
    <FirstName>John</FirstName> 
    <LastName>Doe</LastName> 
    <CustomerEmail>[email protected]</CustomerEmail> 
    </Customer> 
    <Customer> 
    <CustomerId>456</CustomerId> 
    <FirstName>Jane</FirstName> 
    <LastName>Doe</LastName> 
    <CustomerEmail>[email protected]</CustomerEmail> 
    </Customer> 
</sroot> 

希望它有帮助!