我正在使用DataContractSerializer使用DataMember属性将对象序列化为XML(仅公共属性被序列化).
是否可以动态忽略某些属性,以便它们不会包含在XML输出中?
例如,允许用户在某个列表控件中选择所需的xml元素,然后仅序列化用户选择的那些元素,排除所有未选择的元素.
谢谢
我查看了其他帖子,但我找不到解决问题的方法.
我的服务配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<appSettings>
...
</appSettings>
<log4net>
...
</log4net>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="wsDualHttpBinding"/>
</protocolMapping>
<services>
<service name="Service1" behaviorConfiguration="Service1Behavior">
<endpoint address="" binding="netTcpBinding" contract="IService1" bindingConfiguration="NetTcpBinding_IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="/mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
<service name="Service2" behaviorConfiguration="Service2Behavior">
<endpoint address="" binding="wsHttpBinding" contract="IService2" bindingConfiguration="WSHttpBinding_IService2">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService2" receiveTimeout="00:00:10"
sendTimeout="00:00:10" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
useDefaultWebProxy="false">
<reliableSession ordered="true" inactivityTimeout="00:00:11" />
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" …Run Code Online (Sandbox Code Playgroud) c# wcf serialization visual-studio-2010 datacontractserializer
我正在使用带有DataContract序列化的Web API .输出如下所示:
<Data>
<Status>
<Date>2014-08-13T00:30:00</Date>
<ID>312</ID>
<Option>No Limitation</Option>
<Value i:nil="true" />
</Status>
<Status>
<Date>2014-08-13T01:00:00</Date>
<ID>312</ID>
<Option>No Limitation</Option>
<Value i:nil="true" />
</Status>
<Status>
<Date>2014-08-13T01:30:00</Date>
<ID>312</ID>
<Option>No Limitation</Option>
<Value i:nil="true" />
</Status>
<Status>
<Date>2014-08-13T02:00:00</Date>
<ID>312</ID>
<Option>No Limitation</Option>
<Value i:nil="true" />
</Status>
Run Code Online (Sandbox Code Playgroud)
通过执行以下操作,我能够删除所有额外的命名空间:
[DataContract(Namespace="")]
public class Status
Run Code Online (Sandbox Code Playgroud)
但剩下的唯一属性是i:nil属性.我该怎么做才能删除i:nil属性?
我想强制svcutil在WCF使用的程序集中生成所有数据协定,无论某个类型是否由给定的操作契约引用.
[DataContract]
public class Foo { }
[DataContract]
public class Bar : Foo { }
[ServiceContract]
public interface IService
{
[OperationContract]
void Get(Foo foo);
}
Run Code Online (Sandbox Code Playgroud)
鉴于此设置,我无法svcutil生成版本,Bar因为目前没有操作合同引用它.有没有办法强制svcutil生成数据合同Bar?
我理解XMLSerializer如何通过使用反射来确定它应该用于序列化或反序列化XML的公共读/写字段或属性.然而,XMLSerializer要求字段是公共的和读/写.
但是,DataContractSerializer能够读取或写入类中的完全私有字段.所以我想知道如何明确给出DataContractSerializer对我的类的附加访问权限.
DataContractSerializer如果字段不在"正确"(无论是什么意思)顺序中,是否可能错误地反序列化对象?
我尝试序列化/反序列化的类没有在字段/属性上放置顺序属性.然而,我的一个字段总是被反序列化,null即使它具有非空值(它实际上包含字符串列表).
当我在序列化文件中移动两个XML元素以匹配另一个XML示例中的顺序时(反序列化工作没有问题)一切都开始工作.
这对我没有意义,但也许有人知道的更好.;)
.net c# serialization xml-serialization datacontractserializer
我很难找到一个通用扩展方法,它将给定对象序列化为SOAP格式.实际的实现看起来像这样:
Foobar.cs
[Serializable, XmlRoot("foobar"), DataContract]
public class Foobar
{
[XmlAttribute("foo"), DataMember]
public string Foo { get; set; }
[XmlAttribute("bar"), DataMember]
public string Bar { get; set; }
public Foobar() {}
}
Run Code Online (Sandbox Code Playgroud)
Lipsum.cs
[Serializable, XmlRoot("lipsum"), XmlType("lipsum"), DataContract]
public class Lipsum
{
private List<Foobar> lipsum = new List<Foobar>();
[XmlElement("foobar"), DataMember]
public List<Foobar> Lipsum { get { return lipsum; } }
}
Run Code Online (Sandbox Code Playgroud)
}
Extensions.cs
public static void SerializeToSoap<T>(this Stream target, T source)
{
XmlTypeMapping xmlTypeMapping = (new SoapReflectionImporter().ImportTypeMapping(typeof(T)));
XmlSerializer xmlSerializer = …Run Code Online (Sandbox Code Playgroud) 在我的Silverlight 4应用程序中,我使用DataContractSerializer序列化/反序列化数据.我可以有两种不同类型的数据:EditorModel和ConfiguratorModel.两个模型都继承自一个共同的基类.
[DataContract(IsReference = true, Name = "ServiceModel", Namespace = "ServiceModeller.DataModel.Serialization")]
[KnownType(typeof(DTO_ServiceModelEditor))]
[KnownType(typeof(DTO_ServiceModelConfigurator))]
public abstract class DTO_ServiceModelBase { ... }
[DataContract(IsReference = true, Name = "ServiceModelEditor", Namespace = "ServiceModeller.DataModel.Serialization")]
public class DTO_ServiceModelEditor : DTO_ServiceModelBase { ... }
[DataContract(IsReference = true, Name = "ServiceModelConfigurator", Namespace = "ServiceModeller.DataModel.Serialization")]
public class DTO_ServiceModelConfigurator : DTO_ServiceModelBase { ... }
Run Code Online (Sandbox Code Playgroud)
序列化没有问题,并按预期工作.当我反序列化时,我不想命名特定的继承类,因为用户也可以加载EditorModel或ConfiguratorModel.我找到了这个stackoverflowquestion,由Marc Gravell回答,据我所知,我可以在知道继承类型时使用基类(它可以使用它,参见DTO_ServiceModelBase中的KnownType-Declaration).
但是,当我执行以下反序列化时(我还添加了两个继承类型作为第二个参数):
DataContractSerializer serializer = new DataContractSerializer(typeof(DTO_ServiceModelBase), new Type[] {typeof(DTO_ServiceModelEditor), typeof(DTO_ServiceModelConfigurator)} );
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new System.IO.StringReader(stream));
// stream is the serialized string …Run Code Online (Sandbox Code Playgroud) 我在SO处找到了许多解决方案,而且是通过序列化/反序列化(进入内存和后退)来处理对象的深层克隆.
它要求克隆的类标记为[Serializable].我碰巧把我的类(大部分都是)标记为[DataContract]因为我使用DataContractSerializer序列化为XML.
我只介绍了[Serializable]属性,因为需要对其中一些类实例进行深度克隆.但是,现在通过DCS进行序列化/反序列化的事情发生了,因为它不再起作用了 - 关于在反序列化时期望不同的XML元素的错误.如果我删除了[Serializable]错误消失了.
我有什么选择?我只想尽可能简单地深入克隆我的对象.
让我先说我对WCF很新,可能在这里使用了错误的术语.我的项目有两个组成部分:
我有一个XML模式,如下所示:
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="Attachment">
<xsd:complexType>
<xsd:all>
<xsd:element name="Extension" type="Extension" minOccurs="0" />
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:complexType>
<xsd:sequence name="Extension">
<xsd:any processContents="skip" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)
遵循此模式,我有一个以下类型的XML文档:
<Attachment>
<Extension>
<ReportType1>
<Name>Report Type 1 Example</Name>
</ReportType1>
</Extension>
</Attachment>
Run Code Online (Sandbox Code Playgroud)
我在已编译的DLL中有以下类:
public class Attachment
{
public Extension Extension { get; set; }
}
public class Extension
{
[XmlElement(ElementName = "ReportType1", IsNullable = false)]
public ReportType1 ReportType1 { get; set; }
[XmlElement(ElementName = "ReportType2", IsNullable = false)]
public …Run Code Online (Sandbox Code Playgroud)