xmlserializer

change how XmlSerializer serializes empty elements

岁酱吖の 提交于 2019-12-18 05:13:27
问题 I am using the XmlSerializer. It serializes the object just fine but the client requires required empty elements to be in this format <star:Confirm/> . The serializer instead serializes the empty elements to <star:Confirm></star:Confirm> is there a way to change it to serialize the way the client requires. 回答1: After trying different things I accidentally happened upon the solution. I set the XmlElementAttribute.IsNullable to true like the previous answer suggested. [System.Xml.Serialization

Extension method to serialize generic objects as a SOAP formatted stream

感情迁移 提交于 2019-12-18 05:03:33
问题 I'm having a hard time trying to figure out a generic extension method that would serialize a given object as SOAP formatted. The actual implementation looks somewhat like this: Foobar.cs [Serializable, XmlRoot("foobar"), DataContract] public class Foobar { [XmlAttribute("foo"), DataMember] public string Foo { get; set; } [XmlAttribute("bar"), DataMember] public string Bar { get; set; } public Foobar() {} } Lipsum.cs [Serializable, XmlRoot("lipsum"), XmlType("lipsum"), DataContract] public

C# XmlSerializer BindingFailure

不羁的心 提交于 2019-12-17 22:23:22
问题 I get a BindingFailure on a line of code using the XmlSerializer: XmlSerializer s = new XmlSerializer(typeof(CustomXMLSerializeObject)); The assembly with display name CustomXMLSerializeObject.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly XMLSerializeObject.XmlSerializers, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies

Better IXmlSerializable format?

五迷三道 提交于 2019-12-17 21:20:29
问题 I have an interface IInput that is preventing the XmlSerializer from serializing the class natively (because it doesnt like interfaces). I found a hack/workaround that attempts to just create the underlying implementation when deserializing and then casts that back to the interface. The deserializer knows the underlying implementation because its encoded as a attribute AssemblyQualifiedName In order to take advantage of this technique I have to implement IXmlSerializable, but only 1 property

Serializing an array of multiple types using XmlSerializer

谁说我不能喝 提交于 2019-12-17 20:44:08
问题 I'm trying to use XMLSerializer to generate XML such as the following, where the contents of <create> is an array, but the elements can be of differing types (in this case <vendor> , <customer> , and <asset> ). Is this possible? ... <create> <vendor> <vendorid>Unit - A-1212</vendorid> <name>this is the name8</name> <vcf_bill_siteid3>FOOBAR8</vcf_bill_siteid3> </vendor> <customer> <CUSTOMERID>XML121</CUSTOMERID> <NAME>XML Customer 111</NAME> </customer> <asset> <createdAt>San Jose</createdAt>

How do I add a default namespace with no prefix using XMLSerializer

断了今生、忘了曾经 提交于 2019-12-17 15:55:20
问题 I am trying to generate an XML document that contains the default namespace without a prefix using XmlSerializer , e.g. <?xml version="1.0" encoding="utf-8" ?> <MyRecord ID="9266" xmlns="http://www.website.com/MyRecord"> <List> <SpecificItem> Using the following code ... string xmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(typeof(ExportMyRecord)); XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add

XmlSerializer startup HUGE performance loss on 64bit systems

久未见 提交于 2019-12-17 10:44:56
问题 I am experiencing a really HUGE performance loss while calling a simple XmlSerializer.Deserizlize() on a class with lots of fields. NOTE : I'm writing the code without Visual Studio, at home, so it may have some errors. My serializable class is flat and has hundreds of fields: [Serializable] class Foo { public Foo() { } [XmlElement(ElementName = "Field1")] public string Field1; // [...] 500 Fields defined in the same way [XmlElement(ElementName = "Field500")] public string Field500; } My

Wrap properties with CData Section - XML Serialization C#

蓝咒 提交于 2019-12-17 10:09:33
问题 I need to serialize my object in such a way that the properties I want, would get wrapped around CData sections. I was hoping I could do something like this : public class Order { [JsonProperty] public int OrderId { get; set; } [JsonProperty] public string Name { get; set; } [JsonProperty] public int Type { get; set; } [JsonProperty] public decimal Amount { get; set; } [JsonProperty] public DateTime Date { get; set; } [DataMember] [JsonProperty] **[WrapCData]** public List<Option> ListB { get

Deserialize object property with StringReader vs XmlNodeReader

百般思念 提交于 2019-12-17 07:55:28
问题 Why does XmlSerializer populate my object property with an XmlNode array when deserializing an empty typed element using XmlNodeReader instead of an empty string like it does when using StringReader (or XmlTextReader )? The second assertion in the following code sample fails: var doc = new XmlDocument(); doc.Load(new StringReader(@" <Test xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Value xsi:type=""xsd:string"" /> </Test>")); var

Using XmlSerializer to create an element with attributes and a value but no sub-element

[亡魂溺海] 提交于 2019-12-17 07:42:13
问题 Hopefully this should be an easy answer for someone out there (and possibly a dupe), but I can't seem to figure it out. I need to output an element that looks like this: <Quantity foo="AB" bar="CD">37</Quantity> I know how to get this: <Quantity foo="AB" bar="CD"> <qty>37</qty> </Quantity> with a Quantity class containing public int qty; [XmlAttribute] public string foo; [XmlAttribute] public string bar; but then of course whatever variable I insert the quantity into becomes its own sub