基本上我想有一个单一的构造来处理序列化到 JSON 和格式化的 xml。Records 可以很好地序列化到 json 或从 json 序列化。但是 XmlSerializer 需要一个无参数的构造函数。我真的不想经历为这些构造构建类对象的练习(仅限原则)。我希望可以有一些快捷方式将无参数构造函数放到记录上(也许使用 wioth 语句或其他东西)。我无法让它表现 - 社区中有人有运气吗?
module JSONExample
open System
open System.IO
open System.Net
open System.Text
open System.Web
open System.Xml
open System.Security.Authentication
open System.Runtime.Serialization //add assemnbly reference System.Runtime.Serialization System.Xml
open System.Xml.Serialization
open System.Collections.Generic
[<DataContract>]
type ChemicalElementRecord = {
[<XmlAttribute("name")>]
[<field: DataMember(Name="name") >]
Name:string
[<XmlAttribute("name")>]
[<field: DataMember(Name="boiling_point") >]
BoilingPoint:string
[<XmlAttribute("atomic-mass")>]
[<field: DataMember(Name="atomic_mass") >]
AtomicMass:string
}
[<XmlRoot("freebase")>]
[<DataContract>]
type FreebaseResultRecord = {
[<XmlAttribute("code")>]
[<field: DataMember(Name="code") >]
Code:string
[<XmlArrayAttribute("results")>]
[<XmlArrayItem(typeof<ChemicalElementRecord>, ElementName = "chemical-element")>]
[<field: …Run Code Online (Sandbox Code Playgroud) XmlArrayAttribute和XmlArrayItemAttribute有什么区别?请解释双方(即序列化和反序列化).
我想序列化以下内容:
[Serializable]
[DefaultPropertyAttribute("Name")]
[XmlInclude(typeof(ItemInfo))]
[XmlInclude(typeof(ItemInfoA))]
[XmlInclude(typeof(ItemInfoB))]
public class ItemInfo
{
public string name;
[XmlArray("Items"), XmlArrayItem(typeof(ItemInfo))]
public ArrayList arr;
public ItemInfo parentItemInfo;
}
[Serializable]
[XmlInclude(typeof(ItemInfo))]
[XmlInclude(typeof(ItemInfoA))]
[XmlInclude(typeof(ItemInfoB))]
public class ItemInfoA : ItemInfo
{
...
}
[Serializable]
[XmlInclude(typeof(ItemInfo))]
[XmlInclude(typeof(ItemInfoA))]
[XmlInclude(typeof(ItemInfoB))]
public class ItemInfoB : ItemInfo
{
...
}
Run Code Online (Sandbox Code Playgroud)
该类itemInfo描述了一个容器,它可以容纳itemInfo数组列表中的其他对象,该parentItemInfo描述是项信息的父容器.
既然ItemInfoA并且ItemInfoB派生自ItemInfo它们也可以是数组列表的成员parentItemInfo,因此当尝试序列化这些对象(可以在层次结构中保存许多对象)时,它会失败并出现异常
IvvalidOperationException.`there was an error generating the xml file `
Run Code Online (Sandbox Code Playgroud)
我的问题是:
添加ItemInfo类需要哪些属性才能序列化?
注意:仅当使用parentItemInfo或使用arrayList 初始化ItemInfo [A]/[B]时才会出现异常. …
我正在使用XMLSerializer将类序列化为XML.有很多例子可以将XML保存到文件中.但是我想要的是将XML放入字符串而不是将其保存到文件中.
我正在尝试下面的代码,但它不起作用:
public static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(TestClass));
MemoryStream m = new MemoryStream();
ser.Serialize(m, new TestClass());
string xml = new StreamReader(m).ReadToEnd();
Console.WriteLine(xml);
Console.ReadLine();
}
public class TestClass
{
public int Legs = 4;
public int NoOfKills = 100;
}
Run Code Online (Sandbox Code Playgroud)
有想法该怎么解决这个吗 ?
谢谢.
请考虑以下代码:
public class Obj : IObj
{
public string Prop1{get;set;}
public string Prop2{get;set;}
public string Prop3{get;set;}
}
public static void Persist(IObj obj, string fileFullName)
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(fileFullName));
var xmlSerializer = new XmlSerializer(obj.GetType());
using (var fileStream = File.Open(fileFullName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
xmlSerializer.Serialize(fileStream, obj);
fileStream.Close();
}
}
catch (Exception e)
{
//log
}
}
Run Code Online (Sandbox Code Playgroud)
第一次在' Obj '上调用' Persist ' 时,我在磁盘上获得了一个有效的xml文件,它看起来像这样:
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0"?> <Obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Prop1>value1</Prop1> <Prop2>value2</Prop2> <Prop2>value3</Prop3> </Obj>
但是当' Obj ' 第二次调用' Persist '时(例如,在将' value1 '改为' value '之后),会在文件末尾添加一个额外的'>'符号,使其无效. …
我需要在序列化期间生成以下XML :(片段)
<IncidentEvent a:EventTypeText="Beginning" xmlns:a="http://foo">
<EventDate>2013-12-18</EventDate>
<EventTime>00:15:28</EventTime>
</IncidentEvent>
Run Code Online (Sandbox Code Playgroud)
有问题的类看起来像这样:
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
似乎序列化程序注意到命名空间已经在xmlns:root中声明,并且忽略了我的属性.我也尝试过以下方法:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
private XmlSerializerNamespaces _Xmlns;
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Xmlns
{
get
{
if (_Xmlns == null)
{
_Xmlns = new XmlSerializerNamespaces(); …Run Code Online (Sandbox Code Playgroud) 我喜欢 XmlSerialize 的工作方式,如此简单和优雅,并且具有属性 =p 但是,在序列化为 xml 文件之前构建所有对象的集合时,我遇到了内存不足问题。
我正在从 SQL 数据库填充一个对象,并打算使用 XmlSerialize 将该对象写入 XML。它适用于小子集,但如果我尝试从数据库中获取所有对象,则会遇到内存不足异常。
XmlSerialize 是否有某种功能可以让我从数据库中抓取 100 个对象的批次,然后写入它们,抓取下一批 100 个对象并附加到 xml?
我希望我不必陷入 XmlDocument 或需要更多手动编码工作的东西......
我已经能够使用ShouldSerialize 属性模式XmlSerializer来忽略类型的属性ICollection<>.下面是示例代码.如果我XmlIgnore在ListOfTestClassB属性上使用它,但我希望利用ShouldSerialize模式实现相同的功能.如果我运行下面的代码,我得到'System.InvalidOperationException'说:
无法序列化类型为System.Collections.Generic.ICollection`1 [[ConsoleApplication3.TestClassB,ConsoleApplication3,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]的成员ConsoleApplication3.TestClassA.ListOfTestClassB,因为它是一个接口.
任何人都可以突出我错过了什么?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication3
{
[Serializable]
public class TestClassA
{
public int A { get; set; }
public int B { get; set; }
public string C { get; set; }
public TestClassB TestClass { get; set; }
public virtual ICollection<TestClassB> ListOfTestClassB …Run Code Online (Sandbox Code Playgroud) 我使用 Symfony 3 Serializer 并通过 XmlEncoder 将对象转换为 XML。但 XmlEncoder 在 xml prolog 中没有编码类型。如何解决这个问题呢?
我可以在根元素后添加带参数的自定义属性吗?
有没有办法在根元素中设置 xmlns?
我需要这样的 XML 输出:
<?xml version="1.0" encoding="utf-8"?>
<realty-feed xmlns="http://webmaster.yandex.ru/schemas/feed/realty/2010-06">
<generation-date>2010-10-05T16:36:00+04:00</generation-date>
...
</realty-feed>
Run Code Online (Sandbox Code Playgroud)
现在我明白了:
<?xml version="1.0"?>
<realty-feed>
...
</realty-feed>
Run Code Online (Sandbox Code Playgroud)
我的序列化器代码片段:
$xmlEncoder = new XmlEncoder('realty-feed');
$normalizer = new CustomNormalizer();
$serializer = new Serializer(array($normalizer),array($xmlEncoder));
$output = $serializer->serialize($objectData, 'xml');
Run Code Online (Sandbox Code Playgroud) 我将类型定义为Example,如下所示,实例化对象并使用XmlSerializer进行序列化后,我得到的x003A不是colon :
这是我的代码:
public class Example
{
[XmlElement("Node1")]
public string Node1 { get; set; }
[XmlElement("rd:Node2")]
public string Node2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
序列化代码
Example example = new Example { Node1 = "value1", Node2 = "value2" };
XmlSerializerNamespaces namespaceSerializer = new XmlSerializerNamespaces();
namespaceSerializer.Add("rd", @"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Example));
string path = System.Windows.Forms.Application.StartupPath + "//example.xml";
using (StreamWriter writer = new StreamWriter(path))
{
serializer.Serialize(writer, example, namespaceSerializer);
}
Run Code Online (Sandbox Code Playgroud)
预期结果
<?xml version="1.0" encoding="utf-8"?>
<Example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node1>value1</Node1>
<rd:Node2>value2</rd:Node2>
</Example> …Run Code Online (Sandbox Code Playgroud)