xml-serialization

Prevent XmlSerializer from emitting xsi:type on inherited types

血红的双手。 提交于 2019-12-18 03:08:34
问题 I have managed to serialize a class that inherits from a base class to XML. However, the .NET XmlSerializer produces an XML element that looks as follows: <BaseType xsi:Type="DerivedType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> This, however, causes the receiving end of a web service to choke and produce an error that amounts to: sorry we do not know "DerivedType". How can I prevent the XmlSerializer from emitting the xsi:Type attribute? Thanks! 回答1: You can use the XmlType

Omitting XML processing instruction when serializing an object

只谈情不闲聊 提交于 2019-12-18 00:32:53
问题 I'm serializing an object in a C# VS2003 / .Net 1.1 application. I need it serialized without the processing instruction, however. The XmlSerializer class puts out something like this: <?xml version="1.0" encoding="utf-16" ?> <MyObject> <Property1>Data</Property1> <Property2>More Data</Property2> </MyObject> Is there any way to get something like the following, without processing the resulting text to remove the tag? <MyObject> <Property1>Data</Property1> <Property2>More Data</Property2> <

Using JAXB to support schemas with minor variations

依然范特西╮ 提交于 2019-12-17 23:23:22
问题 The Situation I need to support generating XML documents based on schemas that vary only slightly between each other. Specifically, the schemas that I need to support are based on industry standards that change slightly over time and vendors may make their own customized version of them. The Problem I was intending to use JAXB 2 (from Metro) with inheritance as a solution. I expected the package structure to end up something like this: com.company.xml.schema.v1 com.company.xml.schema.v2 com

De/Serialize directly To/From XML Linq

China☆狼群 提交于 2019-12-17 21:56:23
问题 Is there any way to de/serialize an object without round-tripping a XmlDocument/temp string? I am looking for something like the following: class Program { static void Main(string[] args) { XDocument doc = new XDocument(); MyClass c = new MyClass(); c.SomeValue = "bar"; doc.Add(c); Console.Write(doc.ToString()); Console.ReadLine(); } } [XmlRoot(ElementName="test")] public class MyClass { [XmlElement(ElementName = "someValue")] public string SomeValue { get; set; } } I get an error when I do

Can I create an element with forward slash as part of the name

白昼怎懂夜的黑 提交于 2019-12-17 20:33:19
问题 Can i create an element with forward slash as part of the name? <Data> <DataRow> <Orange/Apple> fruit </Orange/Apple> </DataRow> </Data> seems invalid to me but just want to confirm. 回答1: Can i create an element with forward slash as part of the name? No, according to the grammar pulished on the W3.org website it is not a valid character in a name. hexadecimal value 0x20 cannot be included in a name 0x20 is the space character and you will see that it is not on the list of valid NameChars. It

XML-schema/validation: different separator for datatype double

我怕爱的太早我们不能终老 提交于 2019-12-17 19:52:53
问题 I changed the datatype of some parameters within my xsd file from string to their real type, in this case double. now I'm facing the problem that around here the comma is used as a separator and not the point as defined by the w3 (http://www.w3.org/TR/xmlschema-2/#double) causing erros all over during deserialization (C#, VS2008). my question: Can I use the w3 schema for validation but with a different separator for double values? thx for your help 回答1: You cannot do that if you want to

Is there a reason why a base class decorated with XmlInclude would still throw a type unknown exception when serialized?

試著忘記壹切 提交于 2019-12-17 18:02:37
问题 I will simplify the code to save space but what is presented does illustrate the core problem. I have a class which has a property that is a base type. There are 3 derived classes which could be assigned to that property. If I assign any of the derived classes to the container and attempt to serialize the container, the XmlSerializer throws the dreaded: "The type x was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically." However my base

Can I Serialize XML straight to a string instead of a Stream with C#?

*爱你&永不变心* 提交于 2019-12-17 17:56:07
问题 This example uses a StringWriter to hold the serialized data, then calling ToString() gives the actual string value: Person john = new Person(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person)); StringWriter stringWriter = new StringWriter(); xmlSerializer.Serialize(stringWriter, john); string serializedXML = stringWriter.ToString(); Is there any easier/Cleaner way to do this? All of the Serialize() overloads seem to use a Stream or Writer. UPDATE: Asked a similar question

How to keep XmlSerializer from killing NewLines in Strings?

蓝咒 提交于 2019-12-17 17:48:06
问题 Suppose I have a simple Class with just one Member a String. public class Abc { private String text; public String Text { get { return this.text; } set { this.text = value; } } } Now when I serialize and then deserialize it with the questionable XmlSerializer any text containing newlines ('\r\n' or Environment.NewLine) are transformed to '\n'. How do I keep the newlines? 回答1: It is not the XmlSerializer but the XmlWriter which is removing your CR. To retain it we must have the writer convert

How to serialize/deserialize optional XML enumeration in C#?

限于喜欢 提交于 2019-12-17 15:59:14
问题 I am trying to figure out how to serialize/deserialize an XML listing to C# that has an optional attribute that is an enumerated type. The following is my C# class: public class AttributeAssignmentExpressionElement : XACMLElement { [XmlAttribute] public string AttributeId { get; set; } [XmlAttribute] public Category Category { get; set; } } My Category enumeration is defined as follows: public enum Category { [XmlEnum(Name = "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject")]