xml-deserialization

How to ignore unused XML elements while deserializing a document?

大兔子大兔子 提交于 2019-11-30 10:46:13
I'm using SimpleXml to (de)serialize POJOs. Now, I have a big XML which has some elements which are not needed. For instance, with this XML: <Root> <Element>Used</Element> <Another>Not used</Another> <Root> I want to create a POJO which looks like: @Root class Root{ @Element private String element; } Problem is that I'm getting this Exception: simpleframework.xml.core.ElementException: Element 'Another' does not have a match in class blah.blah.Blah at line 1 So... how should I configure the POJO so that I can parse the XML correctly? Set strict to false within the Root annotation to ignore any

Can't use XMLGregorianCalendar in Android, even if it is documented?

こ雲淡風輕ζ 提交于 2019-11-30 05:36:22
问题 I really can't understand this one: it looks like Android has the XMLGregorianCalendar class, because it is documented here. But if you go ahead and try to use it, that's what you get: 10-27 17:21:43.677: E/AndroidRuntime(14850): Caused by: javax.xml.datatype.DatatypeConfigurationException: Provider org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl not found 10-27 17:21:43.677: E/AndroidRuntime(14850): at javax.xml.datatype.DatatypeFactory.newInstance(DatatypeFactory.java:102) This happens

XML deserialization crashes on decimal parse due to formatting

拟墨画扇 提交于 2019-11-30 04:48:20
问题 I get a System.FormatException thrown when i try to parse XML into an object. As far as I can tell, it's due to the culture used in System.Xml.Serialization.XmlSerializer.Deserialize, wich expects a dot as the decimal character, but the xml contains a comma. The object looks as follows: public sealed class Transaction { [XmlElement("transactionDate")] public DateTime TransactionDate { get; set; } [XmlElement("transactionAmount")] public decimal Amount { get; set; } [XmlElement(

How avoid exception deserializing an invalid enum item?

試著忘記壹切 提交于 2019-11-29 14:22:27
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 } When de-serializing previously serialized objects, you get a System.InvalidOperation exception as

Deserialize multiple XML elements with the same name through XmlSerializer class in C#

为君一笑 提交于 2019-11-29 12:16:51
问题 I have an XML in the form <BackupSchedule> <AggressiveMode>0</AggressiveMode> <ScheduleType>0</ScheduleType> <ScheduledDay>0</ScheduledDay> <ScheduledDay>1</ScheduledDay> <ScheduledDay>0</ScheduledDay> <ScheduledDay>0</ScheduledDay> <ScheduledDay>0</ScheduledDay> <ScheduledDay>0</ScheduledDay> <ScheduledDay>0</ScheduledDay> <WindowStart>480</WindowStart> <WindowEnd>1020</WindowEnd> <ScheduleInterval>0</ScheduleInterval> </BackupSchedule> I need to deserialize it, change its contents and than

XML deserialization generic method

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 06:17:36
I have next XML file: <Root> <Document> <Id>d639a54f-baca-11e1-8067-001fd09b1dfd</Id> <Balance>-24145</Balance> </Document> <Document> <Id>e3b3b4cd-bb8e-11e1-8067-001fd09b1dfd</Id> <Balance>0.28</Balance> </Document> </Root> I deserialize it to this class: [XmlRoot("Root", IsNullable = false)] public class DocBalanceCollection { [XmlElement("Document")] public List<DocBalanceItem> DocsBalanceItems = new List<DocBalanceItem>(); } where DocBalanceItem is: public class DocBalanceItem { [XmlElement("Id")] public Guid DocId { get; set; } [XmlElement("Balance")] public decimal? BalanceAmount { get;

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

删除回忆录丶 提交于 2019-11-28 13:59:28
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 displayName="Total Cooling Capacity" type="custom" typeOfParameter="Power" units="W">1758 W</Total

XmlSerializer - Deserialize different elements as collection of same element

牧云@^-^@ 提交于 2019-11-28 13:03:08
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 Width {get;set;} } Is it possible with XmlSerializer class? If you are not allowed to change the schema,

XML Parsing and deserialization

余生颓废 提交于 2019-11-28 12:37:30
I have a xml file which Im reading it from my class <Testclasses> <Class>new SomeClass1()</class> <class>new SomeClass2()</class> </Testclasses> so i have a method in the class which takes an argument as an object as below public List<Object> retriveValuesFromXml(){ .... This method parses the values from xml and reads the different object and returns a list of objects. } @Test public void someMethod1(){ ArrayList<Object> list_of_objects= retriveValuesFromXml(); for(Object x :list_of_objects){ someMethod2(x); //for example : x = new SomeClass1() or x = new SomeClass2() } } public void

fast way to deserialize XML with special characters

痴心易碎 提交于 2019-11-28 09:28:47
I am looking for fast way to deserialize xml, that has special characters in it like ö. I was using XMLReader and it fails to deserialze such characters. Any suggestion? EDIT: I am using C#. Code is as follows: XElement element =.. //has the xml XmlSerializer serializer = new XmlSerializer(typeof(MyType)); XmlReader reader = element.CreateReader(); Object o= serializer.Deserialize(reader); I'd guess you're having an encoding issue, not in the XMLReader but with the XmlSerializer . You could use the XmlTextWriter and UTF8 encoding with the XmlSerializer like in the following snippet (see the