天道酬勤,学无止境

xml-serialization

"406 Not Acceptable" when trying to configure XML marshalling over HTTP for a Spring Web MVC service

A toy example that captures the issue I am having is on github. In a nutshell I have a service that needs to marshal XML back over HTTP. I am trying to set that up using Spring Web MVC. The response object that needs to get marshalled back is JAXB2-generated from a schema (thus is missing @XmlRootElement tag but the package where it is generated has an ObjectFactory which provides the means to generate JAXBElement-s, which make XML marshallers happy). I tried different spring context configurations based on Google searches, which mostly hit posts here on stack overflow, but could not get any

2022-01-18 11:14:23    分类:问答    java   xml   spring   spring-mvc   xml-serialization

InvalidOperationException on a PostAsXmlAsync call

When calling HttpClient method PostAsXmlAsync, I'm getting the following InvalidOperationException: Token EndElement in state End Document would result in an invalid XML document. Why am I getting this exception? var user = new RXGRequestUser(); var client = new HttpClient(); var response = await client.PostAsXmlAsync(@"myurl.com", user); RXGRequestUser: public class RXGRequestUser : IXmlSerializable { public string Username { get; set; } public string Password { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public int

2022-01-18 06:11:47    分类:问答    c#   xml-serialization   dotnet-httpclient

Declare [XmlElement(IsNullable = true)] at class level

Is there a way to declare [XmlElement(IsNullable = true)] at class level so that all properties in the class will be XML serialized, even if they are null? e.g. public BankAccount BankAccount { get; set; } Should result in <BankAccount xsi:nil="true" />, rather than the default missing element. I tried this but the compiler (correctly) states that the attribute is not valid for class declarations. The reason for this is that I don't want to have to specify this for all properties. Edit: This is the serialization method I am using: var serializer = new XmlSerializer(FormType); var stream = new

2022-01-17 16:42:22    分类:问答    c#   attributes   properties   xml-serialization

How to serialize to dateTime

Working to get DateTimes for any time zone. I'm using DateTimeOffset, and a string, and an XmlElement attribute. When I do, I get the following error: [InvalidOperationException: 'dateTime' is an invalid value for the XmlElementAttribute.DataType property. dateTime cannot be converted to System.String.] System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +450 [InvalidOperationException: There was an error reflecting type 'System.String'

2022-01-17 10:03:24    分类:问答    c#   datetime   .net-2.0   xml-serialization

C# Controlling element name when serialising to XML

I have a class that I want to serialise to XML. The name of outer element of the class when serialised needs to be controlled by the application. At design time I know the element name can be controlled by the use an XmlTypeAttribute [XmlElement(Name="MyName")] I need to control this at run time so this will not work for me. I also looked at IXmlSerializable to create my own serialisation code, but again this will not work as this only allows control of the 'internals' of the class and not the external wrapper. Are there any other options available?

2022-01-17 07:27:26    分类:问答    c#   xml   xml-serialization

Custom XML serialization - Include class name

I'm after the following XML serialization output: <?xml version="1.0"?> <Message> <Version>1.0</Version> <Body> <ExampleObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <EmampleOne>Hello!</EmampleOne> </ExampleObject> </Body> </Message> I have the following classes: [Serializable] public class Message<T> { public string Version { get; set; } public T Body { get; set; } } [Serializable] public class ExampleObject { public string EmampleOne { get; set; } } If I serialize them separately I get: <?xml version="1.0"?> <Message> <Version>1.0<

2022-01-16 21:59:01    分类:问答    c#   .net   xml   serialization   xml-serialization

Java DomImplementationLS(Java DomImplementationLS)

问题 我正在寻找在 Java 中创建 XML Document 对象并将它们序列化为字节数组(在通过 TCP 连接发送它们之前)。 我目前的代码如下所示: public byte [] EncapsulateThingy( ThingyType thingy ) { parser.reset(); // parser is a pre-existing DocumentBuilder object Document doc = parser.newDocument(); doc.appendChild( doc.createElement("Thingy") ); // ... add nodes to doc to represent thingy ByteArrayOutputStream outputStream = new ByteArrayOutputStream( 8192 ); // // Missing: Write doc to outputStream with xml version 1.0 and UTF-8 // encoding. // return outputStream.toByteArray(); } Sun Java 文档有关于一组接口的信息,这些接口似乎以 DomImplementationLS 开头,用于加载和保存 XML

2022-01-16 17:42:46    分类:技术分享    java   xml   networking   xml-serialization

如何在没有无参数构造函数的情况下对密封类进行 XML 序列化?(How can I XML Serialize a Sealed Class with No Parameterless Constructor?)

问题 我目前正在使用XMLSerializer来序列化我自己的一个类的列表。 该类的属性之一是没有无参数构造函数的密封类的实例,因此 XML 序列化器拒绝序列化该类。 我怎样才能解决这个问题? 我需要对该属性进行序列化。 我有什么方法可以指定该类应该如何序列化? 我们希望继续使用 XML; 是否有另一个我可以使用的不会出现此问题的 XML 序列化程序? 再次,如果这是一个骗局,我很抱歉,但我不知道要搜索什么。 [编辑]澄清一下,我无权访问密封类的来源。 回答1 不能直接做; XmlSerializer无法处理没有无参数构造函数的类。 我通常做的是将无参数类包装在另一个与 XML 兼容的类中。 包装类有一个无参数的构造函数和一组读写属性; 它有一个调用真实类的构造函数的FromXml方法。 [XmlIgnore] public SomeClass SomeProperty { get; set; } [XmlElement("SomeProperty")] public XmlSomeClass XmlSomeProperty { get { return XmlSomeClass.ToXml(SomeProperty); } set { SomeProperty = value.FromXml(); } } 回答2 你能做一个私有的无参数构造函数吗? 假设您可以访问该类的代码

2022-01-16 15:27:39    分类:技术分享    c#   .net   serialization   xml-serialization   sealed

How can I XML Serialize a Sealed Class with No Parameterless Constructor?

I'm currently using an XMLSerializer to serialize a list of a class of my own. One of the class's properties is an instance of a sealed class that does not have a parameterless constructor, so the XML Serializer refuses to serialize the class. How can I get around this? I need that property to be serialized. Is there some way for me to specify how that class should be serialized? We'd like to stay with XML; is there another XML serializer that I could use that would not have this problem? Again, I apologize if this is a dupe, but I had no idea what to search. [EDIT] To clarify, I don't have

2022-01-16 08:40:38    分类:问答    c#   .net   serialization   xml-serialization   sealed

Android,帮助一个简单的框架 PersistenceException(Android, help with a simpleframework PersistenceException)

问题 我正在尝试使用org.simpleframework.xml 。 在我的 Android 项目中处理 xml 数据的类。 我不明白如何构建我的类“Point”构造器以匹配 xml 定义:在运行时我得到这个异常: org.simpleframework.xml.core.PersistenceException: Constructor not matched for class koine.marcos.wifidemo.Point 我的xml数据是这样的: 文件点.xml: <?xml version="1.0" encoding="utf-8"?> <points> <point id="La Gioconda"> <rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-52</rssi> <rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-97</rssi> </point> <point id="La Pietà"> <rssi ssid="beacon1" bssid="00:21:91:d1:36:62">-68</rssi> <rssi ssid="beacon2" bssid="00:12:a9:03:23:32">-83</rssi> </point> </points>

2022-01-16 08:06:58    分类:技术分享    android   xml   xml-serialization   xml-simple