关于XML序列化和定制

时间:2011-03-16 09:19:01

标签: c# xml-serialization

我正在进行xml序列化,但我需要自定义输出。

我的代码在这里

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
namespace Serialize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Movie movie = new Movie();
            movie.Title = "Starship Troopers";
            movie.ReleaseDate = DateTime.Parse("11/7/1997");
            movie.Rating = 6.9f;

            String XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(typeof(Movie));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            xs.Serialize(xmlTextWriter, movie);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        }

        private String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }
    }


    public class Movie
    {
        string _Title = "";
        DateTime _ReleaseDate;
        float _Rating = 0;

        [XmlElement("MovieName")]
        public string Title
        {
            get
            {
                return _Title;
            }
            set 
            { 
                _Title = value;
            }
        }

        [XmlElement("MovieRating")]
        public float Rating
        {
            get
            {
                return _Rating;
            }
            set
            {
                _Rating = value;
            }
        }

        [XmlElement("MovieReleaseDate")]
        public DateTime ReleaseDate
        {
            get
            {
                return _ReleaseDate;
            }
            set
            {
                _ReleaseDate = value;
            }
        }
    }
}

当我运行此代码时,我得到的输出像

<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MovieName>Starship Troopers</MovieName>
  <MovieRating>6.9</MovieRating>
  <MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>

如果你看到,那么你会注意到我不需要的额外信息 那xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

此信息随电影标记一起出现。所以,我需要在我的代码中更改,因此额外的信息不会显示在代码中。

所以我的代码看起来像

<?xml version="1.0" encoding="utf-8"?>
<Movie>
  <MovieName>Starship Troopers</MovieName>
  <MovieRating>6.9</MovieRating>
  <MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>

请详细帮助纠正。

1 个答案:

答案 0 :(得分:1)

我认为您必须手动删除它们,例如:

How to remove all namespaces from XML with C#?