标签: xmlserializer

XML序列化 - 何时使用DataContractSerializer/Binary/XMLSerialiser

我已经看了一会儿了

似乎不鼓励使用二进制序列化,因为对字段名称的任何更改都会破坏序列化=?不好

XMLSerializer是有问题的,因为您必须提供无参数构造函数和公共字段,尽管您可以更多地控制元素属性或元素及其命名

DataContractSerializer很好,但需要明确添加所有的分类,这是一种耻辱

但是我偶然发现NetDataContractSerializer没有这个限制.

如果你的目标是C#序列化并且对xml的大小没有很大的限制,那么NetDataContractSerializer总是可以去这里吗?

c# datacontractserializer xmlserializer

5
推荐指数
1
解决办法
5939
查看次数

XmlSerializer的.跳过xml未知节点

我的xml文件反序列化有问题.让我们假设我们有一个xml文件和一个我们用于反序列化的类.

例如:

xml -

<dataStore>
  <name>newDataStore1</name>
  <description>sdffasdfasdf</description>
  <type>Shapefile</type>
  <enabled>false</enabled>
  <workspace>
    <name>newTestWorkspace</name>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="ht
tp://192.168.6.71:8080/geoserver/rest/workspaces/newTestWorkspace.xml" type="app
lication/xml"/>
  </workspace>
  <connectionParameters>
    <entry key="memory mapped buffer">false</entry>
    <entry key="create spatial index">true</entry>
    <entry key="charset">ISO-8859-1</entry>
    <entry key="filetype">shapefile</entry>
    <entry key="cache and reuse memory maps">true</entry>
    <entry key="url">file:data/shapefiles/states.shp</entry>
    <entry key="namespace">http://www.opengeospatial.net/cite</entry>
  </connectionParameters>
  <__default>false</__default>
  <featureTypes>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="ht
tp://192.168.6.71:8080/geoserver/rest/workspaces/newTestWorkspace/datastores/new
DataStore1/featuretypes.xml" type="application/xml"/>
  </featureTypes>
</dataStore>
Run Code Online (Sandbox Code Playgroud)

namespace GeoServerApiTester
{


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlRootAttribute("dataStore", Namespace="", IsNullable=false)]
    public partial class DataStore
    {

        private string nameField;

        private string typeField;

        private …
Run Code Online (Sandbox Code Playgroud)

c# xml xml-serialization xmlserializer

5
推荐指数
1
解决办法
5921
查看次数

在JavaScript中,如何将DOM的序列化器部分转换为XHTML?

我想将部分DOM序列化为XHTML(有效的XML).我们假设我里面只有一个元素<body>,这是我要序列化的元素:

<div>
    <hr>
    <img src="/foo.png">
</div>
Run Code Online (Sandbox Code Playgroud)

有了这个,document.innerHTML给我几乎我想要的,除了它返回HTML,而不是XHTML(即,<hr>并且<img>将不会被正确关闭).既然innerHTML没有诀窍,我如何将部分DOM序列化为XHTML?

javascript xhtml dom innerhtml xmlserializer

5
推荐指数
1
解决办法
782
查看次数

如何为可选的XML元素装饰/定义类成员以与XmlSerializer一起使用?

我有以下XML结构.该theElement元素可以包含theOptionalList元素,或不:

<theElement attrOne="valueOne" attrTwo="valueTwo">
    <theOptionalList>
        <theListItem attrA="valueA" />
        <theListItem attrA="anotherValue" />
        <theListItem attrA="stillAnother" />
    </theOptionalList>
</theElement>
<theElement attrOne="anotherOne" attrTwo="anotherTwo" />
Run Code Online (Sandbox Code Playgroud)

什么是表达相应类结构的干净方式?

我很确定以下内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MyNamespace
{
    public class TheOptionalList
    {
        [XmlAttributeAttribute("attrOne")]
        public string AttrOne { get; set; }

        [XmlAttributeAttribute("attrTwo")]
        public string AttrTwo { get; set; }

        [XmlArrayItem("theListItem", typeof(TheListItem))]
        public TheListItem[] theListItems{ get; set; }

        public override string ToString()
        {
            StringBuilder outText = new StringBuilder();

            outText.Append("attrOne = " …
Run Code Online (Sandbox Code Playgroud)

c# xmlserializer

5
推荐指数
2
解决办法
2万
查看次数

MVC4 webapi中的反序列化/模型绑定不适用于数组

我正在使用新的WebApi,它是MVC4 beta的一部分.我有以下课程:

public class Voucher
{
    public string Id { get; set; }
    public string TableId { get; set; }
    public Product[] Products { get; set; } 
}
Run Code Online (Sandbox Code Playgroud)

我的控制器看起来像这样:

public class VouchersController : ApiController
{
    public Voucher PostVoucher(Voucher voucher)
    {
          //....
    }
}
Run Code Online (Sandbox Code Playgroud)

在客户端,我使用一个序列化数据XmlSerializer.输出看起来像预期的并且Products数组是序列化的.如果我发布的数据,并把一个破发点中PostVoucher的方法,我得到的数据IdTableId,但Productsnull.什么提示我可能做错了什么?

c# asp.net-mvc model-binding xmlserializer asp.net-web-api

5
推荐指数
1
解决办法
4360
查看次数

序列化包含对象列表的对象

我心中已经建立NotInheritable Serilizer是serilizes我的所有类以及与成功类的列表.直到我构建一个包含类列表的类列表.我正在获取运行时Exeption:生成XML文档时出错.导致完全空的XML :(

这些是我的serilize课程:

<System.Serializable> _
<System.Xml.Serialization.XmlInclude(GetType(StatisticItem))> _
Public Class Statistic
    Public StatItem As New list(Of StatisticItem)
    'Bla bla bla
end class


<Serializable> _
Public Class StatisticItem              
        Private stStatPath As String = ""    
        Private eStatType As StatType = 0
        Private iBatchNumber As Int32 = 0
end class
Run Code Online (Sandbox Code Playgroud)

和序列化器:

Public NotInheritable Class XmlSerializer       

    Public Shared Sub Serialize(Of T)(ByVal obj As T, sConfigFilePath As String)        
        Dim XmlBuddy As New System.Xml.Serialization.XmlSerializer(GetType(T))
        Dim MySettings As New System.Xml.XmlWriterSettings()
        MySettings.Indent = True
        MySettings.CloseOutput = True       
        Dim …
Run Code Online (Sandbox Code Playgroud)

vb.net serialization xmlserializer

5
推荐指数
1
解决办法
1万
查看次数

XmlException,同时以UTF-16编码格式反序列化xml文件

使用C#的XmlSerializer。

在反序列化给定文件夹中的所有xml文件的过程中,我看到XmlException "There is an error in XML document (0, 0)".和InnerException是"There is no Unicode byte order mark. Cannot switch to Unicode".

。目录中的所有xml 都是“ UTF-16”编码的。唯一的区别是,某些xml文件缺少在反序列化期间我正在使用其对象的类中定义的元素。

例如,考虑我的文件夹中有3种不同类型的xml:

file1.xml

<?xml version="1.0" encoding="utf-16"?>
<ns0:PaymentStatus xmlns:ns0="http://my.PaymentStatus">
</ns0:PaymentStatus>
Run Code Online (Sandbox Code Playgroud)

file2.xml

<?xml version="1.0" encoding="utf-16"?>
<ns0:PaymentStatus xmlns:ns0="http://my.PaymentStatus">
<PaymentStatus2 RowNum="1" FeedID="38" />
</ns0:PaymentStatus>
Run Code Online (Sandbox Code Playgroud)

file3.xml

<?xml version="1.0" encoding="utf-16"?>
<ns0:PaymentStatus xmlns:ns0="http://my.PaymentStatus">
<PaymentStatus2 RowNum="1" FeedID="38" />
<PaymentStatus2 RowNum="2" FeedID="39" Amt="26.0000" />
</ns0:PaymentStatus>
Run Code Online (Sandbox Code Playgroud)

我有一个代表上述xml的类:

[XmlTypeAttribute(AnonymousType = true, Namespace = "http://my.PaymentStatus")]
[XmlRootAttribute("PaymentStatus", Namespace = "http://http://my.PaymentStatus", IsNullable = true)]
public class PaymentStatus
{

    private …
Run Code Online (Sandbox Code Playgroud)

c# xml encoding xmlexception xmlserializer

5
推荐指数
2
解决办法
4742
查看次数

C#Xml序列化:不能(de)序列化来自另一个程序集的对象

我正在尝试在C#中对一个对象进行XML序列化/反序列化.问题是该对象的类型未在与调用序列化的代码相同的程序集中声明.相反,它来自在运行时动态加载的程序集,因此在编译时不知道调用序列化的代码.

我正在尝试序列化的类型是这样的:

//Assembly = P.dll
namespace EDFPlugin.Plugin1
{
    [Serializable]
    [XmlRoot(Namespace = "EDFPlugin.Plugin1")] 
    [XmlInclude(typeof(Options))] 
    public class Options
    {
        private string _username;
        private string _password;

        public string Username {
            get { return _username; }
            set { _username = value;}
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如我之前提到的,我用来尝试序列化/反序列化这个对象的代码位于一个程序集中,该程序集Options在编译时不知道类型(因为它P.dll在运行时动态加载).不过,我设法通过使用以下代码正确地序列化了类型:

//Assembly = A.exe (doesn't know about P.dll at compile time)
object value = GetOptions() //the actual type returned by …
Run Code Online (Sandbox Code Playgroud)

.net c# serialization xml-serialization xmlserializer

5
推荐指数
1
解决办法
1199
查看次数

C#XMLSerializer将错误的类型反序列化为List

下面的程序是我在C#中反序列化XML时发现的一个问题的例子.我有两个单独的程序集,它们声明一个具有相同名称的类型,在下面的示例中为"Country".这些类型由XML命名空间区分.当我反序列化包含单个"Country"元素的配置文件时,将解析正确的"Country"类型.但是,如果我反序列化"Country"元素的"List",则反序列化错误的"Country"类型.

class Program
{
    static void Main(string[] args)
    {
        XDocument gbConfig = XDocument.Parse(@"<TradingBlocConfiguration>
                                                 <GreatBritain>
                                                   <Country/>
                                                   <Countries>
                                                      <Country/>
                                                      <Country/>                                                                        
                                                    </Countries>
                                                  </GreatBritain>                                                                     </TradingBlocConfiguration>");


        XDocument euConfig = XDocument.Parse(@"<TradingBlocConfiguration>
                                                 <EuropeanUnion>
                                                   <Country/>
                                                   <Countries>
                                                      <Country/>
                                                      <Country/>                                                                        
                                                    </Countries>
                                                  </EuropeanUnion>                                                                     </TradingBlocConfiguration>");

        var greatBritainConfiguration = BuildConfig<TradingBlocConfiguration>(gbConfig);

        // A single 'Country' is always deserialized correctly..
        Console.WriteLine("Great Britain Country Type   " + greatBritainConfiguration.TradingBlocConfig.MemberCountry.GetType());

        // A List of 'Country' is deserialized to the wrong type, depending on what '[XmlElement]' tag is listed first.
        Console.WriteLine("Great Britain Countries Type " + greatBritainConfiguration.TradingBlocConfig.MemberCountries[0].GetType());

        var euConfiguration …
Run Code Online (Sandbox Code Playgroud)

c# xml serialization xml-serialization xmlserializer

5
推荐指数
1
解决办法
204
查看次数

C#Xml序列化器将列表反序列化为0而不是null

XmlSerializer对幕后工作方式感到困惑。我有一个将XML反序列化为对象的类。我看到的是以下两个元素,它们不是正在反序列化的Xml的一部分。

[XmlRootAttribute("MyClass", Namespace = "", IsNullable = false)]
public class MyClass
{
    private string comments;
    public string Comments
    {
        set { comments = value; }
        get { return comments; }
    }

    private System.Collections.Generic.List<string> tests = null;
    public System.Collections.Generic.List<string> Tests
    {
        get { return tests; }
        set { tests = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

让我们以以下XML为例:

<MyClass>
  <SomeNode>value</SomeNode>
</MyClass>
Run Code Online (Sandbox Code Playgroud)

您会注意到测试和注释不是XML的一部分。

对该XML进行反序列化时,Comment为null(这是预期的),而Tests是一个空列表,计数为0。

如果有人可以向我解释这一点,将不胜感激。我更希望的是,如果<Tests>XML中缺少该列表,则该列表应保持为空,但是如果存在(可能为空)节点<Tests />,则应分配该列表。

c# xml serialization xmlserializer

5
推荐指数
1
解决办法
655
查看次数