Multiple Root Declarition In Xml File during Serialization
i am doing Xml serialization but stuck in one error which i have almost one week i have simple web page like
FirstNameTextBox: LastNameTextBox: EmailTextBox:
SubmitButton
i have two classes called Customer and a Customer List the customer Class look like this
namespace XmlSearlizeProject.Classes
{
[Serializable]
[XmlRoot("CustomerData")]
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;
}
}
}
}
and a CustomerList Class Look Like this
namespace XmlSearlizeProject.Classes
{
[Serializable]
public class CustomerList:List<Customer>
{
}
}
and the code which perform XML Serialization is:
namespace XmlSearlizeProject.WebPages
{
public partial class CustomerPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void GeneralFunction(Stream xmlStream)
{
string customerId = Guid.NewGuid().ToString();
Classes.CustomerList customerList = new Classes.CustomerList{
new Classes.Customer
{
FirstName = this.FirstNameTextBox.Text,
LastName = this.LastNameTextBox.Text,
Email = this.EmailTextBox.Text,
CustomerId = customerId
}
};
Classes.Customer customer = new Classes.Customer();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Classes.CustomerList));
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("FirstName");
firstName.InnerText = customer.FirstName;
document.DocumentElement.InsertAfter(firstName, document.DocumentElement.LastChild);
XmlElement lastName = document.CreateElement("LastName");
lastName.InnerText = customer.LastName;
document.DocumentElement.InsertAfter(lastName, document.DocumentElement.LastChild);
XmlElement email = document.CreateElement("Email");
email.InnerText = customer.Email;
document.DocumentElement.InsertAfter(email, document.DocumentElement.LastChild);
XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlStream, Encoding.UTF8);
xmlSerializer.Serialize(xmlTextWriter, customerList);
xmlStream.Close();
}
private void SerializeCustomer()
{
Stream xmlWriterStream = new FileStream(Server.MapPath("~/Customer.xml"), FileMode.OpenOrCreate, FileAccess.ReadWri开发者_JS百科te);
GeneralFunction(xmlWriterStream);
xmlWriterStream.Close();
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
SerializeCustomer();
}
}
}
and now my problem is this when i add first customer using Serialization Function then my XML File look like this
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Customer>
<CustomerId>34f15068-4331-4253-8660-d1ce2199ed6d</CustomerId>
<FirstName>haseeb</FirstName>
<LastName>khan</LastName>
<CustomerEmail>abc@hotmtail.com</CustomerEmail>
</Customer>
</ArrayOfCustomer>
But when i try to add another Customer using the same Serialization Function with the same web page then my XML file look like this
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Customer>
<CustomerId>34f15068-4331-4253-8660-d1ce2199ed6d</CustomerId>
<FirstName>haseeb</FirstName>
<LastName>khan</LastName>
<CustomerEmail>abc@hotmtail.com</CustomerEmail>
</Customer>
</ArrayOfCustomer><?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Customer>
<CustomerId>924e5fe3-d5ce-41c2-a850-4d3da1e44f91</CustomerId>
<FirstName>ammar</FirstName>
<LastName>khan</LastName>
<CustomerEmail>xyz@hotmail.com</CustomerEmail>
</Customer>
</ArrayOfCustomer>
i am not understanding that how to fix this error i need my Xml File Like this when i ever i Add New Customer
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Customer>
<CustomerId>34f15068-4331-4253-8660-d1ce2199ed6d</CustomerId>
<FirstName>haseeb</FirstName>
<LastName>khan</LastName>
<CustomerEmail>abc@hotmtail.com</CustomerEmail>
</Customer>
<Customer>
<CustomerId>924e5fe3-d5ce-41c2-a850-4d3da1e44f91</CustomerId>
<FirstName>ammar</FirstName>
<LastName>khan</LastName>
<CustomerEmail>xyz@hotmail.com</CustomerEmail>
</Customer>
</ArrayOfCustomer>
please help i am having this problem for one week and i have tried every thing but able to solve this error if anyone help me i will be thankful to him
精彩评论