xml-serialization

Custom Element Names using the DataContractSerializer on a List of primitives

こ雲淡風輕ζ 提交于 2021-02-04 14:52:07
问题 I'm interested about the best way to go about setting custom element names when using List of primitives with the DataContractSerializer. Let's say I have the following class which contains a List of Strings as a DataMember. [DataContract] public class ClassName { [DataMember] public List<String> FieldName { get; set; } } By default, this serializes to the following: <ClassName xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <FieldName xmlns:a="http://schemas.microsoft.com/2003/10

FlowDocument Xml serialization

北城以北 提交于 2021-01-29 11:10:30
问题 I have FlowDocument as a part of another composite object, what must be serializable. [Serializable] public class RichTextSerializationWrapper { public FlowDocument Document { get; set; } } So, I want serialize RichTextSerializationWrapper as follows: var serializer = new XmlSerializer(typeof(RichTextSerializationWrapper)); TextWriter writer = new StreamWriter(fileName); serializer.Serialize(writer, richTextSerializationWrapper); writer.Close(); And I, of course, get exception: There was an

Returning a dynamic object is throwing runtime error

浪尽此生 提交于 2021-01-28 17:03:24
问题 I am developing a Web API, where the GET method needs to return an object, whose variables will be decided based on an XML file. The returned format must be either XML or JSON as requested by the client. I want to return the data inside XML file into XML format to the client, and something reasonable for JSON when JSON is requested. The nodes in the XML might increase or decrease and therefore I cannot define a fixed class in the Models . My current solution is to return a dynamic object, but

Returning a dynamic object is throwing runtime error

霸气de小男生 提交于 2021-01-28 17:02:48
问题 I am developing a Web API, where the GET method needs to return an object, whose variables will be decided based on an XML file. The returned format must be either XML or JSON as requested by the client. I want to return the data inside XML file into XML format to the client, and something reasonable for JSON when JSON is requested. The nodes in the XML might increase or decrease and therefore I cannot define a fixed class in the Models . My current solution is to return a dynamic object, but

XmlSerializer with parameterless constructor with no public properties or fields… Is it possible?

扶醉桌前 提交于 2021-01-28 14:05:37
问题 Having the following class (.Net 3.5): public class Something { public string Text {get; private set;} private Something() { Text = string.Empty; } public Something(string text) { Text = text; } } This serializes without error but the resulting XML does not include the Text property since it does not have a public setter. Is there a way (the simpler, the better) to have the XmlSerializer include those properties? 回答1: XmlSerializer only cares about public read/write members. One option is to

IXmlSerializable dictionary in C# without 'Key'/'Value' nodes

北城余情 提交于 2021-01-27 23:38:31
问题 I'm trying to serialize a dictionary in C#. All the examples I've been able to find create XML like the following: <Dictionary> <ArrayOfEntries> <Entry> <Key>myFirstKey</Key> <Value>myFirstValue</Value> </Entry> <Entry> <Key>mySecondKey</Key> <Value>mySecondValue</Value> </Entry> </ArrayOfEntries> </Dictionary> It varies, sometimes the ArrayOfEntries node isn't necessary, but I still see the regular pattern of the dictionary's key-value pairs being stored in their own nodes. What I would like

Unexpected node type Element

送分小仙女□ 提交于 2021-01-27 08:34:36
问题 I have the following XML : <Envelope> <Body> <RESULT> <SUCCESS>TRUE</SUCCESS> <RecipientId>9876543210</RecipientId> <ORGANIZATION_ID>12345-67890-b9e6bcd68d4fb511170ab3fcff55179d</ORGANIZATION_ID> </RESULT> </Body> </Envelope> Which I'm trying to deserialize to: [XmlRoot(ElementName = "Envelope")] public class Add_Recipent_response { public string Body { get; set; } public string RESULT { get; set; } public string SUCCESS { get; set; } public string RecipientId { get; set; } public string

Unexpected node type Element

折月煮酒 提交于 2021-01-27 08:32:09
问题 I have the following XML : <Envelope> <Body> <RESULT> <SUCCESS>TRUE</SUCCESS> <RecipientId>9876543210</RecipientId> <ORGANIZATION_ID>12345-67890-b9e6bcd68d4fb511170ab3fcff55179d</ORGANIZATION_ID> </RESULT> </Body> </Envelope> Which I'm trying to deserialize to: [XmlRoot(ElementName = "Envelope")] public class Add_Recipent_response { public string Body { get; set; } public string RESULT { get; set; } public string SUCCESS { get; set; } public string RecipientId { get; set; } public string

lmxl incremental XML serialisation repeats namespaces

故事扮演 提交于 2021-01-27 07:41:59
问题 I am currently serializing some largish XML files in Python with lxml. I want to use the incremental writer for that. My XML format heavily relies on namespaces and attributes. When I run the following code from io import BytesIO from lxml import etree sink = BytesIO() nsmap = { 'test': 'http://test.org', 'foo': 'http://foo.org', 'bar': 'http://bar.org', } with etree.xmlfile(sink) as xf: with xf.element("test:testElement", nsmap=nsmap): name = etree.QName(nsmap["foo"], "fooElement") elem =

lmxl incremental XML serialisation repeats namespaces

被刻印的时光 ゝ 提交于 2021-01-27 07:41:44
问题 I am currently serializing some largish XML files in Python with lxml. I want to use the incremental writer for that. My XML format heavily relies on namespaces and attributes. When I run the following code from io import BytesIO from lxml import etree sink = BytesIO() nsmap = { 'test': 'http://test.org', 'foo': 'http://foo.org', 'bar': 'http://bar.org', } with etree.xmlfile(sink) as xf: with xf.element("test:testElement", nsmap=nsmap): name = etree.QName(nsmap["foo"], "fooElement") elem =