Tag: xml serialization

XMLSerialization:参数对象’Sw’的类型不是原始的

我正在尝试将对象序列化为XML文件,但是我收到了上述错误。 问题似乎是包含基类列表的对象,但是由从基类派生的对象填充。 示例代码如下: public class myObject { public myObject() { this.list.Add(new Sw()); } public List list = new List(); } public class Units { public Units() { } } public class Sw : Units { public Sw(); { } public void main() { myObject myObject = new myObject(); XmlSerializer serializer = new XmlSerializer(typeof(myObject)); TextWriter textWriter […]

xml使用它的generics类型序列化generics类

我需要将包含Pair类型的对象的列表序列化为xml。 除了这些值,我还需要序列化它的generics类型( T和U的类型)。 首先,我创建了一个类PairList来保存对的列表,然后我创建了实际的类,它表示一对两个值,键和值。 [XmlRoot(“pairList”)] public class PairList{ [XmlElement(“element”)] public List<Pair> list; public PairList() { list = new List<Pair>(); } } public class Pair { [XmlAttribute(“key”)] public T key; [XmlAttribute(“value”)] public U value; [XmlAttribute(“T-Type”)] public Type ttype; [XmlAttribute(“U-Type”)] public Type utype; public Pair() { } public Pair(T t, U u) { key = t; value […]

将Class转换为XML为string

我正在使用XMLSerializer将类序列化为XML。 有很多例子可以将XML保存到文件中。 但是我想要的是将XML放入字符串而不是将其保存到文件中。 我正在尝试下面的代码,但它不起作用: public static void Main(string[] args) { XmlSerializer ser = new XmlSerializer(typeof(TestClass)); MemoryStream m = new MemoryStream(); ser.Serialize(m, new TestClass()); string xml = new StreamReader(m).ReadToEnd(); Console.WriteLine(xml); Console.ReadLine(); } public class TestClass { public int Legs = 4; public int NoOfKills = 100; } 有想法该怎么解决这个吗 ? 谢谢。

如何使用DataContractSerializer创建具有与我的已知类型匹配的标记名称的xml

我有以下数据合同: [CollectionDataContract(Name=”MyStuff”)] public class MyStuff : Collection {} [DataContract(Name = “Type1”)] [KnownType(typeof(Type1))] public class Type1 { [DataMember(Name = “memberId”)] public int Id { get; set; } } [DataContract(Name = “Type2”)] [KnownType(typeof(Type2))] public class Type2 { [DataMember(Name = “memberId”)] public int Id { get; set; } } 我将其序列化为xml如下: MyStuff pm = new MyStuff(); Type1 t1 = […]

使用特定方法控制XML序列化格式

我有一个DateTime和一个子类,我希望格式化XML序列化。 通常,在没有指定任何内容的情况下,DateTime的序列化将遵循当前的文化,但我希望以某种方式格式化DateTime(即使不可反序列化),同样是子类。 所以,给定这些类: public class MyClass { public DateTime MyDateTime { get; set; } public MySubClass TheSubClass { get; set; } } public class MySubClass { public int ID { get; set; } public string Name { get; set; } } 如何指定输出的序列化方法: 2011-9-13T10:30:00Z ID-Name

将类的Nullable双重属性Serilize为XmlText

我必须使用以下代码序列化: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Serialization; namespace MyExample { class Program { static void Main(string[] args) { MyXmlDocument document = new MyXmlDocument(); document.MyExample.NodeA.value = “Value To Node A”; document.MyExample.NodeB.value = “Value To Node B”; document.MyExample.NodeC.value = 1234.567; document.WriteToXml(@”C:\Users\E9JR\Desktop\mydocument.xml”); Console.Write(“> Done!”); Console.ReadKey(); } } [XmlRoot(ElementName=”xmlExample”,IsNullable=false)] public class […]

C#xml序列化

我在c#中将对象序列化为xml,我想序列化 public String Marker { get; set; } 成 当字符串标记没有值时。 现在我明白了 for Marker == string.Empty ,没有标记节点为null 。 我怎么能得到这个?

自定义XML序列化 – 包括类名

我正在使用以下XML序列化输出: 1.0 Hello! 我有以下课程: [Serializable] public class Message { public string Version { get; set; } public T Body { get; set; } } [Serializable] public class ExampleObject { public string EmampleOne { get; set; } } 如果我将它们单独序列化,我得到: 1.0 Hello! 和: Hello! 所以如上所示,我希望内部主体包含类名 。 我使用generics,因为我需要有不同的Message Body,我用代码序列化: var obj = new Message { Version = “1.0”, […]

如何序列化到dateTime

努力获取任何时区的DateTimes。 我正在使用DateTimeOffset,一个字符串和一个XmlElement属性。 当我这样做时,我收到以下错误: [InvalidOperationException:’dateTime’是XmlElementAttribute.DataType属性的无效值。 dateTime无法转换为System.String。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)+450 [InvalidOperationException:反映类型’System.String’的错误。] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)+1621 System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping访问器,FieldModel模型,XmlAttributes a,String ns,Type choiceIdentifierType,Boolean rpc,Boolean openModel,RecursionLimiter limiter)+8750 System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent,FieldModel model,XmlAttributes a,String ns,RecursionLimiter limiter)+139 System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping,StructModel model,Boolean openModel,String typeName,RecursionLimiter limiter)+1273 [InvalidOperationException:反映属性’creationTimeX’时出错。] … 码: [System.Xml.Serialization.XmlElement(ElementName = “creationTime”, DataType=”dateTime”)] public string creationTimeX { get […]

哪个更好处理版本控制? XmlSerializer与DataContractSerializer?

需要序列化对象,并且在反序列化时可能会更改程序集版本。 此外,它可能发生,对象改变了一点。 XmlSerializer不存储类型信息,如果对象稍微改变,它就不会失败,但是XmlSerializer不能从超类中序列化私有或内部属性,我无法用属性标记。 所以我看了一下DataContractSerializer。 它看起来很好,超级类的私有/内部属性的问题将得到解决,所有属性都必须标记,我不需要它们,但类型信息怎么样? 如果某些属性被删除,重命名或添加,DataContractSerializer的行为如何?