xml-serialization

Custom Serialization using XmlSerializer

梦想与她 提交于 2019-12-22 18:16:11
问题 I have a class that I need to do some custom XML output from, thus I implement the IXmlSerializable interface. However, some of the fields I want to output with the default serialization except I want to change the xml tag names. When I call serializer.Serialize, I get default tag names in the XML. Can I change these somehow? Here is my code: public class myClass: IXmlSerializable { //Some fields here that I do the custom serializing on ... // These fields I want the default serialization on

Deserialize XML with ampersand using XmlSerializer()

故事扮演 提交于 2019-12-22 15:26:07
问题 The following code breaks when the XML has data like "Lord & Hogan". Any suggestions? Thanks, Ken private T GetResponse<T>(String apiObject, String query) { //Deserialize XML into the type specified. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(BuildRequestUri(apiObject, query)); using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse()) { try { XmlSerializer ser = new XmlSerializer(typeof(T)); return (T)ser.Deserialize(resp.GetResponseStream()); } catch (Exception e)

require either one element or the other in JAXB

浪尽此生 提交于 2019-12-22 14:38:14
问题 I have a JAXB-annotated POJO like this: @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Clazz implements Serializable { @XmlElement(required = false) private int a; @XmlElement(required = false) private int b; } I want to mark that either field a or field b is required. With my current set-up, none of them are required, but I want one of them to be present and not the other. How could I achieve it? 回答1: You can do the following with @XmlElementRefs Domain Model Clazz import

Does XML Serialization Require Properties to be Read/Write?

萝らか妹 提交于 2019-12-22 10:05:32
问题 I'm testing XML Serialization in my class, but i noticed that the ID number didn't get saved when i ran the program. So i was looking around and modifying a few things but nothing worked, then i saw that all fields except ID had both get and set properties. So i added a set; property to my ID number and poof it worked. The question is, does it have to be set; and get; function on all my properties for XML Serialization to work? I don't want the ID number to be modified after the object has

XML deserialize: different xml schema maps to the same C# class

我是研究僧i 提交于 2019-12-22 09:10:12
问题 One of the jobs of my program is to read customer list from a xml file and deserialize them into C# class like below: <?xml version="1.0" encoding="utf-8"?> <customers> <customer> <name>john</name> <id>1</id> </customer> <customer> <name>mike</name> <id>2</id> </customer> </customers> C# class: [XmlRoot("customers")] public class CustomerList { [XmlElement("customer")] public Customer[] Customers { get; set; } } public class Customer { [XmlElement("name")] public String Name {get; set;}

C# object to XML

孤街浪徒 提交于 2019-12-22 08:16:06
问题 I am creating an application which requires to convert c# object to XML. I am using XML Serializer class to achieve this. Here is the code snippet: public class Anwer { public int ID { get; set; } public string XML { get; set; } public Anwer(int ID, string XML) { this.ID = ID; this.XML = XML; } public Anwer() { } } Here is the main function: string AnswerXML = @"<Answer>1<Answer>"; List<Anwer> answerList = new List<Anwer>(); answerList.Add(new Anwer(1,AnswerXML)); AnswerXML = @"<Answer>2

Serializing a DataType=“time” field using XmlSerializer

烈酒焚心 提交于 2019-12-22 06:40:53
问题 I'm getting an odd result when serializing a DateTime field using XmlSerializer. I have the following class: public class RecordExample { [XmlElement("TheTime", DataType = "time")] public DateTime TheTime { get; set; } [XmlElement("TheDate", DataType = "date")] public DateTime TheDate { get; set; } public static bool Serialize( Stream stream, object obj, Type objType, Encoding encoding) { try { var settings = new XmlWriterSettings { Encoding = encoding }; using (var writer = XmlWriter.Create

How to serialize and save an object to database as Xml using Linq to SQL

你说的曾经没有我的故事 提交于 2019-12-22 05:28:08
问题 I were using FileStream to Serialize an Object to Xml and Save to the disk Stream str = new FileStream(@"serializedstate.xml", FileMode.OpenOrCreate) XmlSerializer x = new XmlSerializer(typeof(GridState)); x.Serialize(str, new GridState { GridName= txtGridName.Text, GridColumns = GetGridColumnStates() }); This works fine and Xml file is generated on disk. How do I save serialized Object as Xml into a Sql Server 2008 database's XML column using Linq to SQL? And how to deserialize the same from

Trigger function on deserialization

我与影子孤独终老i 提交于 2019-12-22 04:34:12
问题 I have a class with a number of fields which are normally calculated in the constructor from other data in the class. They are not serialized to XML because any changes to the rest of the data will likely require their recalculation. Is there a way I can set up a function call to be triggered on deserialization? 回答1: What you are describing is [OnDeserialized] XmlSerializer does not support serialization callback methods (at least, not in MS .NET; mono may be different). Depending on your

XML Serialization of the default values of optional attributes

随声附和 提交于 2019-12-22 04:06:26
问题 I have a set of classes build using xsd.exe, and I am trying to serialise them. However, an attribute is not being included in the resulting XML. Here is part of the schema where the problem lies. <xsd:element name="Widget"> <xsd:complexType> /* sequence removed for brevity */ <xsd:attribute name="Version" type="Version" use="optional" default="1.1"/> </xsd:complexType> </xsd:element> <xsd:simpleType name="Version"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="1.0"/> <xsd