i am doing xml serialization but i need to customize the output.
my code is here
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;
}
}
}
}
when i run this code then i am getting the output like
<?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>
here if u see then u will note few extra info is coming which i don't require
that xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
this info is coming along with Movie tag. so please what i need to change in my code as a result the extra info will not show in the code.
so my code will look like
<?xml version="1.0" encoding="utf-8"?>
<Movie>
<MovieName>Starship Troopers</MovieName>
<MovieRating>6.9</MovieRating>
<MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>
please help with rectification in detail.
I think you'll have to remove them manually, like here:
How to remove all namespaces from XML with C#?
精彩评论