我们的Flex应用程序希望将请求和响应作为对象图(在那里没有任何异常)处理,例如响应成为某些视图的模型,并且将是具有多层嵌套的结构.
**现在,理想情况下,我们希望将相同的客户端(和服务器)端对象用于不同的消息格式,例如XML和AMF,并且具有可插入的序列化/反序列化层(!)
AMF使用[RemoteClass(alias ="samples.contact.Contact")]进行客户端到服务器的序列化和匹配,但似乎没有XML的等价物.
我(有点乐观)正在寻找一种将对象图序列化为XML的简洁方法,以便从客户端通过HTTPService发送.
对于响应,默认的"对象"和"E4X"提供了一些反序列化.这很方便,但是我们当然没有将XML解包回特定AS类的细节,就像我们使用AMF一样.
有什么建议?(确实有一个关于将对象包装/转换为XML或XMLList的想法 - 但这似乎不起作用)
更新:
这两个库看起来都很有用,我很可能会在某些时候使用它们.
现在,我真的需要重新使用我们在任何情况下使用的AMF3序列化的元数据集的简单性([RemoteClass],[Transient])
..所以目前最好的选择是AMFX - 使用Flex数据服务进行AMF传输,使用XML - mx.messaging.channels.amfx包中的类 - 目前唯一的缺点是任何Externalizable类都被转换为Hex字节流 - 和ArrayCollection是Externalizable!(希望通过在子类中序列化内部数组来解决方法..)
希望对某人有用..
我有一堂课
[XmlRoot]
<snip>
[XmlAttribute(AttributeName="x:uid")]
public string uid;
<snip>
Run Code Online (Sandbox Code Playgroud)
它在编译时是正常的..但是在运行时,异常发生在行
XmlSerializer serializer = new XmlSerializer(typeof(myClass));
Run Code Online (Sandbox Code Playgroud)
因为"x:uid"中的无效字符..我的类中的元素需要有一个"x:uid"属性用于本地化目的..我该怎么做?
谢谢!
我尝试使用WCF向REST API提交请求; 这就是我所做的:
namespace Sample
{
[ServiceContract]
[XmlSerializerFormat]
public interface ISampleApi
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "users.xml", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
User CreateUser(User user);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的User班级:
namespace Sample.Entity
{
[XmlRoot("user")]
public class User
{
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("country-code")]
public string ContryCode { get; set; }
[XmlElement("created-at")]
public DateTime CreatedAt { get; set; }
[XmlElement("email")]
public string Email { get; set; }
[XmlElement("external-identifier")]
public string ExternalIdentifier { get; set; …Run Code Online (Sandbox Code Playgroud) 我有一个类通过XLINQ实现它自己的(de)序列化,我想让我的类的客户尝试XMLSerialze我的类来调用我的XLINQ方法.
那可能吗?
反序列化XML时遇到问题:
System.InvalidOperationException was unhandled
Message=There is an error in XML document (0, 0).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at xsdToObject.Program.Main(String[] args) in D:\Old Documents\Projects\xsdToObject\xsdToObject\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Xml.XmlException
Message=Root element is missing.
Source=System.Xml
LineNumber=0
LinePosition=0
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception …Run Code Online (Sandbox Code Playgroud) 我有一个枚举:
[DataContract]
public enum Relationship
{
Spouse = 4,
ResidesWith = 1,
Parent = 2,
Other = 3,
PersonalGuarantor = 5,
CoApplicant = 6
}
Run Code Online (Sandbox Code Playgroud)
如您所见,零未定义.我构建了我的程序,认为永远不会在枚举中定义零.这允许我看到哪些已经从各种组合框设置,哪些被设置为空值.如果我将零设置为空值,则无法区分这两个事物,而且我必须能够这样做.
由于缺少默认状态,当我尝试序列化值时出现错误.
有没有办法让我的xml序列化跳过没有值的枚举,或者避免这些错误的方法?我真的不想引入默认值.
我在我的文件中使用XML并想要序列化然后反序列化一个对象,我有些新手.该对象包含一对字符串,一个int,然后是两个int []数组.使用XmlSerializer进行序列化很好,生成的XML看起来像这样:
<?xml version="1.0" encoding="utf-16"?>
<Harvey xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Carl</Name>
<Ch>KNV</Ch>
<Tn>40</Tn>
<APoints>
<int>8</int>
<int>20</int>
<int>16</int>
<int>16</int>
<int>12</int>
<int>12</int>
<int>16</int>
<int>16</int>
<int>4</int>
<int>4</int>
<int>4</int>
</APoints>
<SPoints>
<int>3</int>
<int>12</int>
<int>10</int>
<int>10</int>
<int>6</int>
<int>6</int>
<int>10</int>
<int>10</int>
</SPoints>
</Harvey>
Run Code Online (Sandbox Code Playgroud)
问题是尝试将int []值加载回其数组中.我一直无法弄清楚如何告诉我想所有的Apoints加载到一个int []数组中的应用.我怀疑解决方案非常简单,但我一直无法弄清楚.
我正在使用.Net的Web API编写一个简单的API.我定义了以下模型:
public class VehicleUpdate
{
[Required(ErrorMessage = "DealerID Required")]
public int DealerID { get; set; }
[Required(ErrorMessage = "VIN Required")]
[StringLength(17, ErrorMessage = "VIN Must be 17 characters", MinimumLength = 17)]
public string VIN { get; set; }
[StringLength(8000, ErrorMessage = "Comments must be less than 8,000 characters")]
public string Comments { get; set; }
public double Retail { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过使用以下XML执行HTTP Post来测试它
<VehicleUpdate>
<DealerID>30</DealerID>
<VIN>1FMRU17L0WLA62356</VIN>
<Comments>This is a test.</Comments>
<Retail>1000</Retail>
</VehicleUpdate>
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到以下SerializationException:
System.Runtime.Serialization.SerializationException:第1行位置的错误16.期望来自名称空间'http://schemas.datacontract.org/2004/07/API.Models'的元素'VehicleUpdate'..遇到名为'的'元素' VehicleUpdate',命名空间''.在System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator的XmlReader,布尔verifyObjectName,DataContractResolver dataContractResolver)在System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator读卡器,布尔verifyObjectName,DataContractResolver dataContractResolver)在System.Runtime.Serialization.DataContractSerializer.ReadObject …
我试图使用C#将xml反序列化为对象.
这是我的XML.
<Products>
<Product>
<Name>Test</Name>
<Price Amount="12.95">£ 12.95</Price>
</Product>
</Products>
Run Code Online (Sandbox Code Playgroud)
这是我的代码.
class Program
{
static void Main(string[] args)
{
var filePath = @"C:\Eastpoint\TestApps\TestHunterSuppliers\bin\Debug\Sample.xml";
var reader = new XmlTextReader(filePath);
var serializer = new XmlSerializer(typeof(Products));
var products = (Products)serializer.Deserialize(reader);
Console.WriteLine(products.Product.Name);
Console.WriteLine(products.Product.Price.Amount);
}
}
public class Products
{
public Product Product { get; set; }
}
public class Product
{
public string Name { get; set; }
public Price Price { get; set; }
}
public class Price
{
public string Amount { …Run Code Online (Sandbox Code Playgroud) 我在ApiController中有以下简单代码:
public Version Get()
{
var version = new System.Version(1, 1, 0, 0);
return version;
}
Run Code Online (Sandbox Code Playgroud)
这导致以下结果:
JSON
{"_Major":1,"_Minor":1,"_Build":0,"_Revision":0}
Run Code Online (Sandbox Code Playgroud)
XML
<Version xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/System">
<_Build>0</_Build>
<_Major>1</_Major>
<_Minor>1</_Minor>
<_Revision>0</_Revision>
</Version>
Run Code Online (Sandbox Code Playgroud)
请注意,酒店前面有一个_.仅查看所有这些属性的类定义VersionGetters,我怀疑它与序列化添加的原因有关_.我也试图在这里找到有关为什么会发生这种情况的信息,但所有这些都说明了这一点Read-only properties are serialized by default.
问题是下划线搞乱了客户端的反序列化并导致版本0.0.0.0.
这是一个CLR类,我无法覆盖,所以如何删除下划线以便在客户端上正确反序列化?
serialization json xml-serialization json.net asp.net-web-api