C# XmlSerializer添加属性
我有这个项目类别:C# XmlSerializer添加属性,c#,.net,winforms,C#,.net,Winforms,我有这个项目类别: public class Movie { public string VideoId { get; set; } public string Title { get; set; } } 我有这些项目的列表,我使用这些代码序列化为xml文件: string fileName = index + ".xml"; string serializationFile = Path.Combine(dir, fileName); XmlWriterSettings s
public class Movie
{
public string VideoId { get; set; }
public string Title { get; set; }
}
我有这些项目的列表
,我使用这些代码序列化为xml文件:
string fileName = index + ".xml";
string serializationFile = Path.Combine(dir, fileName);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (var writer = XmlWriter.Create(serializationFile, settings))
{
var serializer = new XmlSerializer(typeof(List<Movie>));
serializer.Serialize(writer, tmpList);
}
你可以在序列化之后再做。代码框架如下所示:
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (var writer = XmlWriter.Create(ms, settings))
{
var serializer = new XmlSerializer(typeof(List<Movie>));
serializer.Serialize(writer, tmpList);
}
ms.Position = 0;
XDocument doc = XDocument.Load(new XmlTextReader(ms));
doc.Root.Add(new XAttribute("customAttribute", "Yes"));
doc.Save(filename);
}
使用(MemoryStream ms=new MemoryStream())
{
XmlWriterSettings=新的XmlWriterSettings();
settings.Indent=true;
使用(var writer=XmlWriter.Create(ms,设置))
{
var serializer=newxmlserializer(typeof(List));
serializer.Serialize(writer,tmpList);
}
ms.Position=0;
XDocument doc=XDocument.Load(新的XmlTextReader(ms));
doc.Root.Add(新的XAttribute(“customAttribute”、“Yes”);
文件保存(文件名);
}
您可能希望将列表
包装在类中,然后将其序列化。
类似于下面的内容
class Program
{
static void Main(string[] args)
{
string fileName = "abcd2.xml";
string serializationFile = Path.Combine(@"C:\", fileName);
List<Movie> tmpList = new List<Movie>();
tmpList.Add(new Movie() { VideoId = "1", Title = "Hello" });
tmpList.Add(new Movie() { VideoId = "2", Title = "ABCD" });
MovieList list = new MovieList("Yes", tmpList);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (var writer = XmlWriter.Create(serializationFile, settings))
{
var serializer = new XmlSerializer(typeof(MovieList));
serializer.Serialize(writer, list);
}
}
}
public class MovieList
{
private string custom;
private List<Movie> movies;
public MovieList() { }
public MovieList(string custom, List<Movie> movies)
{
this.movies = movies;
this.custom = custom;
}
[XmlAttribute]
public string CustomAttribute
{
get { return this.custom; }
set { this.custom = value; }
}
public List<Movie> Movies
{
get
{
return movies;
}
set
{
this.movies = value;
}
}
}
public class Movie
{
public string VideoId { get; set; }
public string Title { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
字符串fileName=“abcd2.xml”;
string serializationFile=Path.Combine(@“C:\”,文件名);
List tmpList=新列表();
添加(newmovie(){VideoId=“1”,Title=“Hello”});
tmpList.Add(新电影(){VideoId=“2”,Title=“ABCD”});
电影列表=新电影列表(“是”,tmpList);
XmlWriterSettings=新的XmlWriterSettings();
settings.Indent=true;
使用(var writer=XmlWriter.Create(序列化文件、设置))
{
var serializer=newxmlserializer(typeof(MovieList));
序列化器。序列化(编写器,列表);
}
}
}
公共级电影演员
{
私人字符串自定义;
私人电影名单;
公共电影列表(){}
公共电影列表(字符串自定义、列表电影)
{
这部电影=电影;
这个习惯=习惯;
}
[XmlAttribute]
公共字符串自定义属性
{
获取{返回this.custom;}
设置{this.custom=value;}
}
公开电影名单
{
得到
{
返回电影;
}
设置
{
这个。电影=价值;
}
}
}
公映
{
公共字符串VideoId{get;set;}
公共字符串标题{get;set;}
}
代码可以改进很多。这只是一个示例片段。查看以下MSDN链接:是的,您可以使用。为此,需要定义自定义属性。它附带了表示数组(嵌套在根节点中)的另一个类的价格。如果您对此添加没有问题,那么解决方案可以如下所示:
<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" customattribute='Yes'>
public class ArrayOfMovie
{
// define the custom attribute
[XmlAttribute(AttributeName="CustomAttribute")]
public String Custom { get; set; }
// define the collection description
[XmlArray(ElementName="Items")]
public List<Movie> Items { get; set; }
}
public class Movie
{
public string VideoId { get; set; }
public string Title { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
CustomAttribute="yes">
<Items>
<Movie>
<VideoId>1</VideoId>
<Title>Movie 1</Title>
</Movie>
<Movie>
<VideoId>2</VideoId>
<Title>Movie 2</Title>
</Movie>
</Items>
</ArrayOfMovie>
公共类ArrayOfMovie
{
//定义自定义属性
[XmlAttribute(AttributeName=“CustomAttribute”)]
公共字符串自定义{get;set;}
//定义集合描述
[XmlArray(ElementName=“Items”)]
公共列表项{get;set;}
}
公映
{
公共字符串VideoId{get;set;}
公共字符串标题{get;set;}
}
然后像您已经做的那样创建、填充和序列化-一个新的事情是填充您的自定义属性:
// create and fill the list
var tmpList = new List<Movie>();
tmpList.Add(new Movie { VideoId = "1", Title = "Movie 1" });
tmpList.Add(new Movie { VideoId = "2", Title = "Movie 2" });
// create the collection
var movies = new ArrayOfMovie
{
Items = tmpList,
Custom = "yes" // fill the custom attribute
};
// serialize
using (var writer = XmlWriter.Create(serializationFile, settings))
{
var serializer = new XmlSerializer(typeof(ArrayOfMovie));
serializer.Serialize(writer, movies);
}
//创建并填写列表
var tmpList=新列表();
tmpList.Add(新电影{VideoId=“1”,Title=“电影1”});
tmpList.Add(新电影{VideoId=“2”,Title=“电影2”});
//创建集合
var movies=新ArrayOfMovie
{
项目=tmpList,
Custom=“yes”//填写自定义属性
};
//连载
使用(var writer=XmlWriter.Create(序列化文件、设置))
{
var serializer=newxmlserializer(typeof(ArrayOfMovie));
连载器。连载(作家、电影);
}
XML输出如下所示:
<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" customattribute='Yes'>
public class ArrayOfMovie
{
// define the custom attribute
[XmlAttribute(AttributeName="CustomAttribute")]
public String Custom { get; set; }
// define the collection description
[XmlArray(ElementName="Items")]
public List<Movie> Items { get; set; }
}
public class Movie
{
public string VideoId { get; set; }
public string Title { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
CustomAttribute="yes">
<Items>
<Movie>
<VideoId>1</VideoId>
<Title>Movie 1</Title>
</Movie>
<Movie>
<VideoId>2</VideoId>
<Title>Movie 2</Title>
</Movie>
</Items>
</ArrayOfMovie>
1.
电影一
2.
电影2
如果类实现任何IList或ICollection,XmlSerializer将忽略任何XmlAttribute成员。在我看来,这就是提出这个问题的原因。@Dmitry:是的,你是对的,当XmlAtributes从IList或ICollection派生时,它会被忽略。但是将列表包装在类中是可行的。编辑答案。问题是,在哪里尝试过这种包装,需要一些其他的解决方案。