本文整理汇总了C#中System.Xml.Serialization.XmlAnyElementAttributes.Add方法的典型用法代码示例。如果您正苦于以下问题:C# XmlAnyElementAttributes.Add方法的具体用法?C# XmlAnyElementAttributes.Add怎么用?C# XmlAnyElementAttributes.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.XmlAnyElementAttributes
的用法示例。
在下文中一共展示了XmlAnyElementAttributes.Add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
public class Group{
public string GroupName;
[XmlAnyElement]
public object[]Things;
}
public class Test{
static void Main(){
Test t = new Test();
// 1 Run this and create the XML document.
// 2 Add new elements to the XML document.
// 3 Comment out the new line, and uncomment
// the DeserializeObject line to deserialize the
// XML document and see unknown elements.
t.SerializeObject("UnknownElements.xml");
// t.DeserializeObject("UnknownElements.xml");
}
private void SerializeObject(string filename){
XmlSerializer ser = new XmlSerializer(typeof (Group));
TextWriter writer = new StreamWriter(filename);
Group g = new Group();
g.GroupName = "MyGroup";
ser.Serialize(writer, g);
writer.Close();
}
private void DeserializeObject(string filename){
XmlSerializer ser = CreateOverrideSerializer();
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
Group g = (Group)
ser.Deserialize(fs);
fs.Close();
Console.WriteLine(g.GroupName);
Console.WriteLine(g.Things.Length);
foreach(XmlElement xelement in g.Things){
Console.WriteLine(xelement.Name + ": " + xelement.InnerXml);
}
}
private XmlSerializer CreateOverrideSerializer(){
XmlAnyElementAttribute myAnyElement =
new XmlAnyElementAttribute();
XmlAttributeOverrides xOverride =
new XmlAttributeOverrides();
XmlAttributes xAtts = new XmlAttributes();
xAtts.XmlAnyElements.Add(myAnyElement);
xOverride.Add(typeof(Group), "Things", xAtts);
return new XmlSerializer(typeof(Group) , xOverride);
}
}