本文整理汇总了C#中System.Xml.Serialization.XmlRootAttribute类的典型用法代码示例。如果您正苦于以下问题:C# XmlRootAttribute类的具体用法?C# XmlRootAttribute怎么用?C# XmlRootAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XmlRootAttribute类属于System.Xml.Serialization命名空间,在下文中一共展示了XmlRootAttribute类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Group
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
[XmlRoot(Namespace = "www.contoso.com",
ElementName = "MyGroupName",
DataType = "string",
IsNullable=true)]
public class Group
{
private string groupNameValue;
// Insert code for the Group class.
public Group()
{
}
public Group(string groupNameVal)
{
groupNameValue = groupNameVal;
}
public string GroupName
{
get{return groupNameValue;}
set{groupNameValue = value;}
}
}
public class Test
{
static void Main()
{
Test t = new Test();
t.SerializeGroup();
}
private void SerializeGroup()
{
// Create an instance of the Group class, and an
// instance of the XmlSerializer to serialize it.
Group myGroup = new Group("Redmond");
XmlSerializer ser = new XmlSerializer(typeof(Group));
// A FileStream is used to write the file.
FileStream fs = new FileStream("group.xml",FileMode.Create);
ser.Serialize(fs,myGroup);
fs.Close();
Console.WriteLine(myGroup.GroupName);
Console.WriteLine("Done");
Console.ReadLine();
}
}