xmlserializer

.NET - Is it possible to use both XmlAnyElementAttribute and XmlSerializer.UnknownElement event within the same object

末鹿安然 提交于 2019-12-24 07:44:56
问题 I have a class in which I had to change te type of a property from a simple List<string> to a complex List<CustomObject> . My problem is that for some period of time, I will have people using the old and the new version of the software. Up until now, when I had contract changes, I simply used the UnknownElement event to map the old member to the new one since it was private files and it works perfectly for backward compatibility but broke the old version since it didn't write the old format

Deserialize XML with XmlSerializer where XmlElement names differ but have same content

亡梦爱人 提交于 2019-12-24 07:14:54
问题 I would like to deserialize an XML File to a class with several subclasses. The XML looks like this: <?xml version="1.0" encoding="utf-8"?> <Objects> <Group index="1"> <de> <GroupName>ANTRIEB</GroupName> </de> <en> <GroupName>missing translation!</GroupName> </en> <Level>2</Level> </Group> <Group index="2"> <de> <GroupName>BREMSEN</GroupName> </de> <Level>3</Level> </Group> </Objects> Deserializing the XML to classes would be no problem, if there wouldn't be those language tags. Sure, I could

Why is a field in an auto-generated class serialized into an element when defined as an attribute in the XSD?

我怕爱的太早我们不能终老 提交于 2019-12-24 06:37:22
问题 I'm trying to expose a fairly complex object as XML through a REST API (using WCF). However, the object is defined by an XSD file so I've generated classes using the xsd.exe tool. The problem is that it seems that when my object is serialized to XML, an attribute (defined in the xsd) is serialized into an element. And I don't get why. Currently, I'm assuming that my xsd somehow allows that, but I can't tell why. I don't do any custom serialization, I let the framework handle it. Can someone

Problems with the DataContractSerialiser and Serializable

ぐ巨炮叔叔 提交于 2019-12-24 04:33:14
问题 I've a few Classes from a asembly wich is .Net 2.0. These Classes are makred with Serializable. In my Project, i uses this classes in my Classes, wich are marked with DataContract(IsReference=true) and DataMember. Now I have the Problem, with DataContractSerialiser that it serialises the private fields of my .Net 2.0 Classes, wich will not work. But when I use XMLSerialiser, i cannot use IsReference, and so I can also not do this. Is there a easy (simple) Solutiuon for this? Maybe a someone

Prevent XmlSerializer from auto instantiating List's on Deserialize

落爺英雄遲暮 提交于 2019-12-24 02:38:06
问题 Assuming I have this sample class: public class MyClass { public List<int> ListTest { get; set; } public string StringTest { get; set; } public int IntTest { get; set; } } And this code: string xmlStr = "<MyClass><StringTest>String</StringTest></MyClass>"; XElement xml = XElement.Parse(xmlStr); XmlSerializer ser = new XmlSerializer(typeof(MyClass)); using (XmlReader reader = xml.CreateReader()) { var res = ser.Deserialize(reader); } After the Deserialize is completed the value of res is:

Stackoverflow Exception when serializing class

给你一囗甜甜゛ 提交于 2019-12-24 02:33:19
问题 I have a tree and want to serialize them to xml. The nodes derive from a Nodebase class (found here I think), which fails on serializing. public class NodeBase : IEqualityComparer, IEnumerable, IEnumerable<NodeBase> { public NodeBase Parent { get; private set; } private readonly IList<NodeBase> children = new ObservableCollection<NodeBase>(); public NodeBase this[int index] { get { return this.children[index]; } } public void AddChild(NodeBase childNode, int index = -1) { if (index < -1) {

XmlSerializer and IEnumerable: Serialization possible w/o parameterless constructor: Bug?

ε祈祈猫儿з 提交于 2019-12-24 00:52:54
问题 In our project we extensivly use the XmlSerializer. By chance I found a class w/o a parameterless contructor. I thought this must break the serialization process but it did not. By investigating this issue I found out, that the XmlSerializer behaves strange when serializing/deserializing an IEnumerable : All elements of the enumerable are serialized The class is required to implement an Add(object) method It ignores all other properties that may be in this class. It calls a getter with this

Deserializing duplicate XML elements with unique attributes

走远了吗. 提交于 2019-12-24 00:12:25
问题 I have the following XML structure: <Response> <Value Name="ID">1</Value> <Value Name="User">JSmith</Value> <Value Name="Description">Testing 123</Value> </Response> How can I deserialize this so that the value names are properties on the class, and the text values are the values of the properties? Note that the value names never change, so Name="ID" will always exist, for example. Here is my class so far: [Serializable] [XmlRoot("Response")] public class ReportingResponse { // [What goes

XmlSerializer.FromTypes producing memory leaks?

无人久伴 提交于 2019-12-23 19:12:29
问题 According to MSDN XmlSerializer Class section "Dynamically Generated Assemblies" the XmlSerializer produces memory leaks when using certain constructors. I wonder if the XmlSerializer.FromTypes(Type[]) method produces memory leaks, too? EDIT: I'm experiencing memory problems, when using the FromTypes method. So i started some investigations: for (int i = 0; i < 1000; i++) { DummyObject content = new DummyObject() { Age = 29, Location = "London", Name = "Pete" }; XmlSerializer serializer =

java XMLSerializer avoid Complex Empty Elements

南笙酒味 提交于 2019-12-23 17:37:22
问题 I've got this code: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document xmldoc = impl.createDocument(null, null, null); Element root = xmldoc.createElement("root"); Element textElement = xmldoc.createElement("text"); Text textNode = xmldoc.createTextNode(""); root.appendChild(textElement); textElement.appendChild(textNode); OutputFormat of = new