本文整理汇总了C#中System.Xml.Serialization.XmlAttributes.XmlRoot属性的典型用法代码示例。如果您正苦于以下问题:C# XmlAttributes.XmlRoot属性的具体用法?C# XmlAttributes.XmlRoot怎么用?C# XmlAttributes.XmlRoot使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Xml.Serialization.XmlAttributes
的用法示例。
在下文中一共展示了XmlAttributes.XmlRoot属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class Group
{
public string GroupName;
[XmlAttribute]
public int GroupCode;
}
public class Test
{
public static void Main()
{
Test t = new Test();
t.SerializeObject("OverrideRoot.xml");
}
// Return an XmlSerializer for overriding attributes.
public XmlSerializer CreateOverrider()
{
// Create the XmlAttributes and XmlAttributeOverrides objects.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlRootAttribute xRoot = new XmlRootAttribute();
// Set a new Namespace and ElementName for the root element.
xRoot.Namespace = "http://www.cpandl.com";
xRoot.ElementName = "NewGroup";
attrs.XmlRoot = xRoot;
/* Add the XmlAttributes object to the XmlAttributeOverrides.
No member name is needed because the whole class is
overridden. */
xOver.Add(typeof(Group), attrs);
// Get the XmlAttributes object, based on the type.
XmlAttributes tempAttrs;
tempAttrs = xOver[typeof(Group)];
// Print the Namespace and ElementName of the root.
Console.WriteLine(tempAttrs.XmlRoot.Namespace);
Console.WriteLine(tempAttrs.XmlRoot.ElementName);
XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
return xSer;
}
public void SerializeObject(string filename)
{
// Create the XmlSerializer using the CreateOverrider method.
XmlSerializer xSer = CreateOverrider();
// Create the object to serialize.
Group myGroup = new Group();
myGroup.GroupName = ".NET";
myGroup.GroupCode = 123;
// To write the file, a TextWriter is required.
TextWriter writer = new StreamWriter(filename);
// Serialize the object and close the TextWriter.
xSer.Serialize(writer, myGroup);
writer.Close();
}
}