本文整理汇总了C#中System.Xml.Serialization.XmlAttributes.XmlText属性的典型用法代码示例。如果您正苦于以下问题:C# XmlAttributes.XmlText属性的具体用法?C# XmlAttributes.XmlText怎么用?C# XmlAttributes.XmlText使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Xml.Serialization.XmlAttributes
的用法示例。
在下文中一共展示了XmlAttributes.XmlText属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class Group
{
public string GroupName;
// This field will be serialized as XML text.
public string Comment;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("OverrideText.xml");
test.DeserializeObject("OverrideText.xml");
}
// Return an XmlSerializer to be used for overriding.
public XmlSerializer CreateOverrider()
{
// Create the XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes xAttrs = new XmlAttributes();
/* Create an XmlTextAttribute and assign it to the XmlText
property. This instructs the XmlSerializer to treat the
Comment field as XML text. */
XmlTextAttribute xText = new XmlTextAttribute();
xAttrs.XmlText = xText;
xOver.Add(typeof(Group), "Comment", xAttrs);
// Create the XmlSerializer, and return it.
return new XmlSerializer(typeof(Group), xOver);
}
public void SerializeObject(string filename)
{
// Create an instance of the XmlSerializer class.
XmlSerializer mySerializer = CreateOverrider();
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an instance of the class that will be serialized.
Group myGroup = new Group();
// Set the object properties.
myGroup.GroupName = ".NET";
myGroup.Comment = "Great Stuff!";
// Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup);
writer.Close();
}
public void DeserializeObject(string filename)
{
XmlSerializer mySerializer = CreateOverrider();
FileStream fs = new FileStream(filename, FileMode.Open);
Group myGroup = (Group)
mySerializer.Deserialize(fs);
Console.WriteLine(myGroup.GroupName);
Console.WriteLine(myGroup.Comment);
}
}