xml-serialization

How force a maximum string length in a C# web service object property?

In this class for example, I want to force a limit of characters the first/last name can allow. public class Person { public string FirstName { get; set; } public string LastName { get; set; } } Is there a way to force the string limit restriction for the first or last name, so when the client serializes this before sending ...

How to add XML serialization instructions without modifying the class

Is there a clever way of adding XML serialization instructions without modifying the serialized class? I don’t like the default serialization and I can’t modify the class. I was considering inheriting the class, and using Shadows (VB.NET) to re-implement the properties (with the serialization instructions), but it results in a lot of du...

How do I write objects for easy XML Serialization in VB.NET?

I'm writing a small application in VB.NET and I would like some of the classes to be able to write themselves out to XML to serve as a "save" feature. I have seen XSD files used to generate VB classes that can serialize themselves into and out of XML very easily. How would I do this if I do have any pre-existing XML format that I need ...

Which is the best alternative for Java Serialization?

I'm currently working on a project which needs to persist any kind of objects (of which implementation we don't have any control) so these objects could be recovered afterwards. We can't implement a ORM because we can't restrict the users of our library at development time. Our first alternative was to serialize it with the Java defau...

XmlSerializer Serialize empty variable to use both tags?

I want to be able to load a serialized xml class to a Soap Envelope. I am starting so I am not filling the innards so it appears like: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" /> I want it to appear like: <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" ></Envelope>` The class I wrote is t...

How to serialize an object to XML without getting xmlns="..."?

Is there a way for me to serialize an object in .NET without the XML Namespaces automatically serializing also? It seems that by default .NET believes the XSI and XSD namespaces should be included, but I don't want them there. ...

Can I fail to deserialize with XmlSerializer in C# if an element is not found?

I am using XmlSerializer to write and read an object to xml in C#. I currently use the attributes XmlElement and XmlIgnore to manipulate the serialization of the object. If my xml file is missing an xml element that I require, my object still deserializes (xml -> object) just fine. How do I indicate (preferably via Attributes) that a...

Why XML-Serializable class need a parameterless constructor

I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object obj) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, obj); return writer.ToString(); } } If the argumen...

Flex - XML Serialization and De-Serialization of nested Object structures

Our Flex app would like to work with requests and responses as object graphs (nothing unusual there) e.g. response becomes the model of some view, and would be a structure with several layers of nesting. ** Now, ideally we would like to use the same client (and server) side objects for different message formats e.g. XML and AMF, and hav...

How do I deserialize this simple xml config with the XmlSerializer class?

I have the following xml I'd like to deserialize into a class <?xml version="1.0" encoding="utf-8" ?> <root> <element1>String1</element1> <element2>String2</element2> </root> I am trying to serialize it into the following class: [XmlRoot("root")] public class root { [XmlElement("element1")] internal st...

JAXB: How to ignore namespace during unmarshalling XML document?

My schema specifies a namespace, but the documents don't. What's the simplest way to ignore namespace during JAXB unmarshalling (XML -> object)? In other words, I have <foo><bar></bar></foo> instead of, <foo xmlns="http://tempuri.org/"&gt;&lt;bar&gt;&lt;/bar&gt;&lt;/foo&gt; ...

Proper way to implement IXmlSerializable?

Once a programmer decides to implement IXmlSerializable, what are the rules and best practices for implementing it? I've heard that GetSchema() should return null and ReadXml should move to the next element before returning. Are these true? And what about WriteXml: should it write a root element for the object or is it assumed that the r...

.NET XML Serializer with Japanese chars in username

We are having a problem whenever our application makes use of the XML Serializer, when we are logged in as a user who has a username containing Japanese characters. We have prepared a sample application that tests the serializer on its own: TestClass myClass = new TestClass(); myClass.MyString = "Hello Wo...

XmlSerializer and XmlElement fields

Hi, I have an xml that looks like this: <Config> <A></A> <Template><B/><C/></Template> </Config> and I would like to deserialize it in to get the <Template><B/><C/></Template> bit as a XmlElement or XmlNode. But when I try like this: public class Config { public string A; public XmlElement Template; } the Template is set...

Flag certain properties as CDATA elements for serialization

When serializing/de-serializing certain classes I've come across the need to flag or mark certain properties as CDATA elements (due to their content). I am currently handling this like so: <XmlElement("MessageText")> _ Public Property XmlContentLeft() As XmlCDataSection Get Dim doc As New XmlDataDocument() ...

How to combine multiple properties into one tag via overriding object serialization

When I serialize; public class SpeedDial { public string Value { get; set; } public string TextTR { get; set; } public string TextEN { get; set; } public string IconId { get; set; } } It results: <SpeedDial> <Value>110</Value> <TextTR>Yangın</TextTR> <TextEN>Fire</TextEN> <IconId>39</IconId> </SpeedDia...

How can I specify the name of the element when a object will serialize

I have the following class [XmlRoot(ElementName= "webSites")] //No capital w at the beginning public class WebSites : List<WebSite> { } public class WebSite { [XmlAttribute("name")] public string Name { set; get; } [XmlAttribute("url")] public String Url { set; get; } } this is serialized to <?xml version="1.0"...

XML Serialization issue for minoccurs

I have a .NET Web app which consumes a Java-based Web service. One of the objects, named Optional, contains search criteria fields. The schema is the following: <xsd:complexType name="Optional"> <xsd:sequence> <xsd:element name="FromAmount" nillable="true" type="xsd:float" minOccurs="0" /> <xsd:element name="ToAmount" nillable="tr...

C# serializing Class to XML where one of class properties is DateTime. How to make this property in ISO format?

Hello! I'm serializing class which contains DateTime property. public DateTime? Delivered { get; set; } After serializing Delivered node contains DateTime formatted like this: 2008-11-20T00:00:00 How can I change this property to make it look like this: 2008-11-20 00:00:00 Thanks in advance ...

XML Deserialization Issue

I have the following xml that's sent to me from a web service. I'm using .NET to deserialize it, but I'm getting an exception saying that its formatted wrong. There is an error in XML document (2, 2) Now, if I understand that correctly, it's not liking that it's finding the first <error> node. <?xml version="1.0" encoding="UTF-8"?> <m...