xml-deserialization

How avoid exception deserializing an invalid enum item?

╄→гoц情女王★ 提交于 2019-11-28 08:04:41
问题 For simplicity purposes here, I will show my sample code using fruit. In actuality I am doing something more meaningful (we hope). Let say we have an enum: public enum FruitType { Apple, Orange, Banana } And a class: [Serializable] public class Fruit { public FruitType FruitType { get; set; } public Fruit(FruitType type) { this.FruitType = type; } } We can serialize and de-serialize it. Now, lets revise the enum, so that it is now: public enum FruitType { GreenApple, RedApple, Orange, Banana

deserializing enums

蓝咒 提交于 2019-11-27 23:33:13
I have an xml in which one of the elements has an attribute that can be blank. For e.g., <tests> <test language=""> ..... </test> </tests> Now, language is enum type in the classes created from the schema. It works fine if the language is specified, it fails to deserialize if it is blank (as shown in example). Edit: Code for deserialization: XmlSerializer xmlserializer = new XmlSerializer(type); StringReader strreader = new StringReader(stringXML); Object o = serializer.Deserialize(strreader); How can I handle this scenario You could declare the enum property as nullable: public Language?

XmlSerializer - Deserialize different elements as collection of same element

梦想的初衷 提交于 2019-11-27 19:28:25
问题 I have the following XML part which schema I can't change. NUMBER, REGION, MENTION, FEDERAL are columns: <COLUMNS LIST="20" PAGE="1" INDEX="reg_id"> <NUMBER WIDTH="3"/> <REGION WIDTH="60"/> <MENTION WIDTH="7"/> <FEDERAL WIDTH="30"/> </COLUMNS> I want to deserialize it to public List<Column> Columns {get;set;} property. So element name would go to Column.Name. Column class: public class Column { //Name goes from Element Name public string Name {get;set;} [XmlAttribute("WIDTH")] public int

Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'Type[]' to 'Type'?

纵然是瞬间 提交于 2019-11-27 18:57:07
I get this error after I created a class from my xsd file using the xsd.exe tool. So I searched the net and found a solution. Here is the link: http://satov.blogspot.com/2006/12/xsdexe-generated-classes-causing.html Problem is that this makes the code run, but somehow the deserialized data seems corrupt. I did what the site suggests and in the end the 2nd array dimension is always empty (see the comments of the site, somebody also had this problem). Question is, how do I solve this issue now? Is there another tool to create the xsd file? I tried Xsd2Code, without success. Thanks :-) Ajax You

Deserialization error in XML document(1,1)

回眸只為那壹抹淺笑 提交于 2019-11-27 17:42:08
问题 I have an XML file that I deserialize, the funny part is the XML file is the was serialized using the following code: enter code here var serializer = new XmlSerializer(typeof(CommonMessage)); var writer = new StreamWriter("OutPut.txt"); serializer.Serialize(writer, commonMessage); writer.Close(); And i m trying to deserialized it again to check if the output match the input. anyhow here is my code to deserialize: var serializer = new XmlSerializer(typeof(CommonMessage)); var reader = new

Why does the OnDeserialization not fire for XML Deserialization?

随声附和 提交于 2019-11-27 15:00:35
I have a problem which I have been bashing my head against for the better part of three hours. I am almost certain that I've missed something blindingly obvious... I have a simple XML file: <?xml version="1.0" encoding="utf-8"?> <WeightStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Records> <Record actual="150" date="2010-05-01T00:00:00" /> <Record actual="155" date="2010-05-02T00:00:00" /> </Records> </WeightStore> I have a simple class structure: [Serializable] public class Record { [XmlAttribute("actual")] public double weight {

Deserializing XML from String

老子叫甜甜 提交于 2019-11-27 11:47:39
问题 I'm trying to convert the result I get from my web service as a string and convert it to an object. This is the string I'm getting from my service: <StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem> So I have a class for this as: [XmlRoot] public class StatusDocumentItem { [XmlElement] public string DataUrl; [XmlElement] public string

XmlSerializer - node containing text + xml + text

孤人 提交于 2019-11-27 08:15:22
问题 I have a node that can contain either text or xml, text + xml or text + xml + text. The first two cases i have solved using [XmlAnyElement] [EditorBrowsable(EditorBrowsableState.Never)] public XmlElement[] TextNodes { get; set; } [XmlText] public string InnerText { get; set; } [XmlIgnore] public string Text { get { return String.Format("{0}{1}", InnerText, TextNodes.Aggregate(String.Empty, (current, documentNode) => current + documentNode.OuterXml)); } } However, the last case where there is

How to deserialize xml elements that have different names, but the same set of attributes to a typed array/collection

主宰稳场 提交于 2019-11-27 08:04:34
问题 This is a part of XML file I'm trying to de-serialize: <entry> ... <A:family type="user"> <A:variationCount>7</A:variationCount> <A:part type="user"> <title>94 LPS</title> <Voltage type="custom" typeOfParameter="Electrical Potential" units="V">120 V</Voltage> <Unit_Length displayName="Unit Length" type="custom" typeOfParameter="Length" units="mm">540</Unit_Length> <Unit_Height displayName="Unit Height" type="custom" typeOfParameter="Length" units="mm">222</Unit_Height> <Total_Cooling_Capacity

using XmlArrayItem attribute without XmlArray on Serializable C# class

谁说我不能喝 提交于 2019-11-27 03:42:54
I want XML in the following format: <configuration><!-- Only one configuration node --> <logging>...</logging><!-- Only one logging node --> <credentials>...</credentials><!-- One or more credentials nodes --> <credentials>...</credentials> </configuration> I'm trying to create a class Configuration that has the [Serializable] attribute. To serialize the credentials nodes, I have the following: [XmlArray("configuration")] [XmlArrayItem("credentials", typeof(CredentialsSection))] public List<CredentialsSection> Credentials { get; set; } However, when I serialize this to XML, the XML is in the