ixmlserializable

IXmlSerializable, reading xml tree with many nested elements

萝らか妹 提交于 2019-12-11 07:38:34
问题 Could you guys give me an example how to read and write from/to xml like this: <Foolist> <Foo name="A"> <Child name="Child 1"/> <Child name="Child 2"/> </Foo> <Foo name = "B"/> <Foo name = "C"> <Child name="Child 1"> <Grandchild name ="Little 1"/> </Child> </Foo> <Foolist> 回答1: Does the element name really change per level? If not, you can use a very simple class model and XmlSerializer . Implementing IXmlSerializable is... tricky; and error-prone. Avoid it unless you absolutely have to use

Deserializing collection of types implementing IXmlSerializable runs forever

感情迁移 提交于 2019-12-11 06:31:28
问题 I have a class implementing IXmlSerializable. This class contains a few properties. Serializing and Deserializing a single instance of the class works fine. But in case of collection of the class, Serialization works fine but Deserialization runs forever. Here is a code snippet. I am using .Net 4.6.2. public class MyClass : IXmlSerializable { public int A { get; set; } public int B { get; set; } public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { this.A =

WCF service xsd after IXmlSerializable implementation is not correct

非 Y 不嫁゛ 提交于 2019-12-11 05:48:39
问题 I have a wcf service (using xmlserialization). There is some class which looks in SoapUI like this: <MyClass> <propertyA>?</propertyA> <propertyB>?</propertyB> </MyClass> I had to implement IXmlSerializable interface on it. After doing it, class has strange structure in SoapUI: <MyClass> <xs:schema> <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]--> </xs:schema> <!--You may enter ANY elements at this point--> </MyClass> May it be a result of the following implementation of the

Serializing IXmlSerializable Types Using XmlSerializer in WCF

纵然是瞬间 提交于 2019-12-11 04:38:33
问题 Thoroughly frustrated with the DataContractSerializer, I'm trying to get up and running in WCF using IXmlSerializable types with XmlSerializer. I've implemented IXmlSerializable and XmlSchemaProvider in my class to be serialized and declared [XmlSerializerFormat] for my OperationContract. Using a complex schema, I get the following error on trying to view the WSDL: "Schema type information provided by IXmlSerializable is invalid: Reference to undeclared attribute group

Custom serialization of a dictionary fails when xml has indentation/line break

柔情痞子 提交于 2019-12-10 22:25:33
问题 In order to have a cleaner XML of a Dictionary serialization, I wrote a custom class, that implements IXmlSerializable . My custom class is defined like this: public class MyCollection : System.Collections.Generic.Dictionary<string, string>, IXmlSerializable { private const string XmlElementName = "MyData"; private const string XmlAttributeId = "Id"; public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { reader.MoveToContent(); while (reader.Read()) { if(reader

Custom XmlSerialization for nested / child objects

牧云@^-^@ 提交于 2019-12-09 08:23:47
问题 I have a scenario in which I have a class Resource which has two other classes nested in it; Action and ResourceURL. I need to write custom xmlserializer for Resource and Action but not for ResourceURL. I implemented IXmlSerializable for both. The problem is, when Resource is serialized, i call the Action.WriteXML(XmlWriter) to get the serialized form of Action, but i can't get serialized form of ResourceURL. The tags become all messed up and it also adds an tag. So how do i serialize an

XmlSerializer saves null-file

泄露秘密 提交于 2019-12-08 02:49:43
问题 I have a problem with serializing my objects. I implemented IXmlSerializable interface and initialize object of XmlSerializer(for example, serializer). But sometimes after calling serializer.Serialize(writer, data) my output file looks like this: why do I have such behavior? public class MyClass : IData { private static readonly XmlSerializer _formatter = new XmlSerializer(typeof(MyData)); public void Save(string filePath) { using (StreamWriter writer = new StreamWriter(filePath)) { Save

XmlSerializer saves null-file

泄露秘密 提交于 2019-12-06 07:33:15
I have a problem with serializing my objects. I implemented IXmlSerializable interface and initialize object of XmlSerializer(for example, serializer). But sometimes after calling serializer.Serialize(writer, data) my output file looks like this: why do I have such behavior? public class MyClass : IData { private static readonly XmlSerializer _formatter = new XmlSerializer(typeof(MyData)); public void Save(string filePath) { using (StreamWriter writer = new StreamWriter(filePath)) { Save(writer); writer.Close(); } } public void Save(TextWriter Writer) { MyData data = GetMyDataObject();

Implementing IXmlSerializable Requires Collection Property to Have Setter

不羁岁月 提交于 2019-12-01 21:18:20
I have a collection property that is of a custom type which inherits from BindingList. Currently, this property gets serialized via XmlSerializer even though it has no Setter. I now am trying to implement IXmlSerializable on this custom collection class and see that the WriteXml() and ReadXml() interface methods only get called if my collection property has a Setter. Why does serialization ignore this property now unless there is a Setter when before it serialized correctly without one. To Reproduce: First, have a class called "Item": public class Item { public Item() {} // Generates some

IXmlSerializable Dictionary problem

女生的网名这么多〃 提交于 2019-12-01 20:28:13
问题 I was trying to create a generic Dictionary that implements IXmlSerializable (credit to Charles Feduke). Here is my trial: Sub Main() Dim z As New SerializableDictionary(Of String, String) z.Add("asdf", "asd") Console.WriteLine(z.Serialize) End Sub Result: <?xml version="1.0" encoding="utf-16"?><Entry key="asdf" value="asd" /> I placed a breakpoint on top of the WriteXml method and I see that when it stops, the writer contains no data at all, and IMHO it should contain the root element and