序列化期间的多个XML名称空间

时间:2011-09-13 09:51:57

标签: c# xml serialization

我编写了一个XML序列化代码,它首先插入了非常好的记录,但是当我添加另一条记录时,它会像我这样生成我的xml文件

<?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>069aa0ea-5470-4ac2-b600-78e23a7b5233</CustomerId>
    <FirstName>hello</FirstName>
    <LastName>world</LastName>
    <CustomerEmail>hell</CustomerEmail>
  </Customer>
</ArrayOfCustomer> 

和我的C#代码是

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

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

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

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

            XmlNode 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.ReadWrite);
            GeneralFunction(xmlWriterStream);
            xmlWriterStream.Close();
        }



        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            SerializeCustomer();
        }

我的问题是我应该如何删除XML文件中的多个命名空间?因为我希望在使用序列化的文本框的帮助下逐个添加多个客户。请有人帮我纠正我的XML文件我被卡住了我没有找到办法让这件事情正确。

1 个答案:

答案 0 :(得分:0)

以下是使用System.Xml.Linq实现此目的的方法:

var xmlElementsCustomers = new XElement("Customer", new object[]
                                {
                                  new XElement("Id", customer.Id),
                                  new XElement("FirstName", customer.FirstName),
                                  new XElement("LastName", customer.LastName),
                                  new XElement("CustomerEmail", customer.CustomerEmail)
                                });
var root = new XElement("ArrayOfCustomer", xmlElementsCustomers);
var myXml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), root);
using (var xmlWriter = XmlWriter.Create(stream))
{
  myXml.Save(xmlWriter);
}