xmlserializer

Ignore property of a property in Xml Serialization in .NET using XmlSerializer

三世轮回 提交于 2019-12-01 18:36:55
I am carrying out Xml serrialization using XmlSerializer . I am carrying out serialization of ClassA , which contains property named MyProperty of type ClassB . I don't want a particular property of ClassB to be serialized. I have to use XmlAttributeOverrides as the classes are in another library. If the property was in ClassA itself, it would have been straightforward. XmlAttributeOverrides xmlOver = new XmlAttributeOverrides(); XmlAttributes xmlAttr = new XmlAttributes(); xmlAttr.XmlIgnore = true; xmlOver.Add(typeof(ClassA), "MyProperty", xmlAttr); XmlSerializer ser = new XmlSerializer

XmlSerializer: keep unknown elements

房东的猫 提交于 2019-12-01 18:17:39
I have a class that is serialized into/deserialized from XML and stored in/restored from a file: public class Customer { public string FirstName; public string LastName; public Customer() { } public Customer(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } public static Customer Load(TextReader reader) { XmlSerializer deserializer = new XmlSerializer(typeof(Customer)); return (Customer)deserializer.Deserialize(reader); } public void Save(TextWriter writer) { XmlSerializer serializer = new XmlSerializer(GetType()); serializer.Serialize(writer, this); } } In a

Xml deserialization appends to list

元气小坏坏 提交于 2019-12-01 18:13:41
I'm trying to deserialize some settings from an xml file. The problematic property/underlying field is one called AlertColors . I initialize the underlying field to white, yellow, and red to make sure that a new instance of this class has a valid color setting. But when I deserialize, _colorArgb ends up with six values, first three are the initialization values and the last three are the ones that are read from the xml file. But the property AlertColors do not append to the field, but changes its elements. Why do I end up with a field with six colors? Here's the code: private List<int>

Xml deserialization appends to list

懵懂的女人 提交于 2019-12-01 18:04:19
问题 I'm trying to deserialize some settings from an xml file. The problematic property/underlying field is one called AlertColors . I initialize the underlying field to white, yellow, and red to make sure that a new instance of this class has a valid color setting. But when I deserialize, _colorArgb ends up with six values, first three are the initialization values and the last three are the ones that are read from the xml file. But the property AlertColors do not append to the field, but changes

How to output hex numbers via XML Serialization in c#?

孤人 提交于 2019-12-01 17:14:13
问题 I've got a few classes and structures that I use XML serialization to save and recall data, but a feature that I'd like to have is to output integers in hex representation. Is there any attribute that I can hang on these structure to make that happen? 回答1: There's a bit of code smell, but the following will work: public class ViewAsHex { [XmlIgnore] public int Value { get; set; } [XmlElement(ElementName="Value")] public string HexValue { get { // convert int to hex representation return Value

.net XmlSerialize throws “WriteStartDocument cannot be called on writers created with ConformanceLevel.Fragment”

ε祈祈猫儿з 提交于 2019-12-01 15:31:39
问题 Am trying to serialize a class, writing to an XML file as multiple fragments, i.e, write each object of the class as an individual fragment, without the XML header/root. Below is a sample code: [Serializable] public class Test { public int X { get; set; } public String Y { get; set; } public String[] Z { get; set; } public Test() { } public Test(int x, String y, String[] z) { X = x; Y = y; Z = z; } } class Program { static void Main(string[] args) { Test t1 = new Test(1, "t1", new[] { "a", "b

Is XmlRootAttribute inheritable?

拥有回忆 提交于 2019-12-01 15:14:23
I have a class I am serializing with C#'s XmlSerializer . It is marked with the XmlRoot attribute, and I would like to inherit this attribute in a derived class. Looking at the documentation it does not say that XmlRoot sets Inherit to false with AttributeUsageAttribute (Inherit is supposed to default to true), but I get an error when trying to deserialize my inherited class without an XmlRoot attribute ("<rootNode xmlns=''> was not expected."). This currently works: [Serializable()] [XmlRoot("rootNode")] public class BaseClass { [XmlAttribute("attributeA")] public int A { get; set; } }

Why can XmlSerializer serialize abstract classes but not interfaces?

谁说胖子不能爱 提交于 2019-11-30 23:05:21
问题 Edit This code should illustrate the whole problem: [XmlInclude(typeof(AThing1))] public abstract class AThing { public abstract string Name { get; set; } } [XmlInclude(typeof(IThing1))] public interface IThing { string Name { get; set; } } public class AThing1 : AThing { public override string Name { get; set; } } public class IThing1 : IThing { public string Name { get; set; } } List<AThing> aThings = new List<AThing>(new AThing[] { new AThing1() { Name = "Bob" } }); List<IThing> iThings =

How to customise the XML output of a Jersey JAXB serialisation

删除回忆录丶 提交于 2019-11-30 22:33:59
I have some @javax.xml.bind.annotation.Xml... annotated classes here intended for a RESt web service. Jersey is setup in a spring managed web container and the web service is returning a well formatted xml. We use the maven-enunciate-plugin to document the web service and create the xsd to the returned xml documents. I now would like to use the documentation xsd file as a schemaLocation within the returned xml file so that the xml validation won't complain about missing definions. How can I get the XML serialisation configured for this? If I remember correctly, I had to do a few of things to

Safest place for the XmlSerializer to save temp files

China☆狼群 提交于 2019-11-30 17:25:18
问题 It's come to my attention that the XmlSerializer needs to use disk space to do its bidding. If there is no writeable %temp% folder, then it fails with an error as follows: Source : System.Xml Message : Unable to generate a temporary class (result=1). error CS2001: Source file 'C:\Windows\TEMP\c1ls4elp.0.cs' could not be found error CS2008: No inputs specified StackTrace : at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters,