xml-serialization

How do I control the <?xml ?> part of xml serialization with .NET?

好久不见. 提交于 2019-12-29 05:56:25
问题 I am using this method to serialize my object: public static string XmlSerialize(object o) { var stringWriter = new StringWriter(); var xmlSerializer = new XmlSerializer(o.GetType()); xmlSerializer.Serialize(stringWriter, o); string xml = stringWriter.ToString(); stringWriter.Close(); return xml; } It makes XML that starts like this: <?xml version="1.0" encoding="utf-16"?> <MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> But I want

how to customise default Boost xml Serialisation default node naming to make it more readable

只愿长相守 提交于 2019-12-29 01:46:30
问题 The code below generates a xml file but , when it loops theough a map , it always names the map key as first and value as second Is there a way that we can customise tag names first and second to groupid and groupType as shown in desired output #include <fstream> #include <boost/serialization/map.hpp> #include <boost/serialization/nvp.hpp> #include <boost/archive/xml_oarchive.hpp> #include <string> #include <iostream> #include <map> using namespace std; class MyConnections { public:

Can I provide custom serialization for XmlSerializer without implementing IXmlSerializable?

强颜欢笑 提交于 2019-12-28 12:24:05
问题 We're using XmlSerializer , and I want to provide custom serialization for certain classes. However, I don't always have the ability to modify the source code of the class in question, otherwise I could just make it implement IXmlSerializable . Is there any way to do this? 回答1: Here's a simple example of the proxy deserialize helper: Given a type that we cannot directly control serialization of at the class level: public sealed class Class //contrived example { public string Property {get;set

How do I use an XmlSerializer to deserialize an object that might be of a base or derived class without knowing the type beforehand?

风格不统一 提交于 2019-12-28 11:53:44
问题 In C#, how do I use an XmlSerializer to deserialize an object that might be of a base class, or of any of several derived classes without knowing the type beforehand? All of my derived classes add additional data members. I've made a simple GUI that can serialize and deserialize class objects. It will serialize objects as whatever inherited class (or even just the base class) is appropriate based on which fields the user chooses to populate. I have no issues with the serialization; the

How do I use an XmlSerializer to deserialize an object that might be of a base or derived class without knowing the type beforehand?

☆樱花仙子☆ 提交于 2019-12-28 11:53:08
问题 In C#, how do I use an XmlSerializer to deserialize an object that might be of a base class, or of any of several derived classes without knowing the type beforehand? All of my derived classes add additional data members. I've made a simple GUI that can serialize and deserialize class objects. It will serialize objects as whatever inherited class (or even just the base class) is appropriate based on which fields the user chooses to populate. I have no issues with the serialization; the

Serialize object to XmlDocument

爱⌒轻易说出口 提交于 2019-12-28 05:30:33
问题 In order to return useful information in SoapException.Detail for an asmx web service, I took an idea from WCF and created a fault class to contain said useful information. That fault object is then serialised to the required XmlNode of a thrown SoapException . I'm wondering whether I have the best code to create the XmlDocument - here is my take on it: var xmlDocument = new XmlDocument(); var serializer = new XmlSerializer(typeof(T)); using (var stream = new MemoryStream()) { serializer

Customize XML Serialize With new Tags And Attributes And Root

拈花ヽ惹草 提交于 2019-12-28 04:27:06
问题 This is my Type: public class MyObject { public string destAdd { get; set; } public long Time { get; set; } public int maxNumb { get; set; } public Account AccountCredentials { get; set; } public System.String Serialize() { String result = ""; XmlSerializer xs = new XmlSerializer(typeof(MyObject)); MemoryStream ms = new MemoryStream(); xs.Serialize(ms, this); result = System.Text.Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); ms.Dispose(); xs = null; return result; } public static

Serializing DateTime to time without milliseconds and gmt

我们两清 提交于 2019-12-28 04:18:28
问题 I have created a C# class file by using a XSD-file as an input. One of my properties look like this: private System.DateTime timeField; [System.Xml.Serialization.XmlElementAttribute(DataType="time")] public System.DateTime Time { get { return this.timeField; } set { this.timeField = value; } } When serialized, the contents of the file now looks like this: <Time>14:04:02.1661975+02:00</Time> Is it possible, with XmlAttributes on the property, to have it render without the milliseconds and the

C# Xml Serialization & Deserialization

不问归期 提交于 2019-12-28 04:08:30
问题 I am trying to serialize an object & save it into a Sql server 2008 xml field. I also have some deserialization code that re-hydrates the object. I am able to serialize & save the object into the db, but get a "Root element missing" exception. [XmlRoot("Patient")] public class PatientXml { private AddressXml _address = null; private EmergencyContactXml _emergencyContact = null; private PersonalXml _personal = null; [XmlElement] public PersonalXml Personal { get { return _personal; } set {

Using XmlSerializer with an array in the root element

邮差的信 提交于 2019-12-28 04:01:27
问题 I have an XML document similar to the following: <scan_details> <object name="C:\Users\MyUser\Documents\Target1.doc"> ... </object> <object name="C:\Users\MyUser\Documents\Target2.doc"> ... </object> ... </scan_details> I am hoping to use the System.Xml.Serialization attributes to simplify XML deserialization. The issue I have is I cannot work out how to specify that the root node contains an array. I have tried creating the following classes: [XmlRoot("scan_details")] public class