Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Syndication / SyndicationElementExtension.cs / 1 / SyndicationElementExtension.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Syndication { using System.Xml; using System.Runtime.Serialization; using System.Xml.Serialization; using System.Diagnostics.CodeAnalysis; using System.ServiceModel.Dispatcher; using System.IO; using System.ServiceModel.Web; public class SyndicationElementExtension { XmlBuffer buffer; int bufferElementIndex; // extensionData and extensionDataWriter are only present on the send side object extensionData; ExtensionDataWriter extensionDataWriter; string outerName; string outerNamespace; public SyndicationElementExtension(XmlReader xmlReader) { if (xmlReader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("xmlReader"); } SyndicationFeedFormatter.MoveToStartElement(xmlReader); this.outerName = xmlReader.LocalName; this.outerNamespace = xmlReader.NamespaceURI; this.buffer = new XmlBuffer(int.MaxValue); using (XmlDictionaryWriter writer = this.buffer.OpenSection(XmlDictionaryReaderQuotas.Max)) { writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag); writer.WriteNode(xmlReader, false); writer.WriteEndElement(); } buffer.CloseSection(); buffer.Close(); this.bufferElementIndex = 0; } public SyndicationElementExtension(object dataContractExtension) : this(dataContractExtension, (XmlObjectSerializer) null) { } public SyndicationElementExtension(object dataContractExtension, XmlObjectSerializer dataContractSerializer) : this(null, null, dataContractExtension, dataContractSerializer) { } public SyndicationElementExtension(string outerName, string outerNamespace, object dataContractExtension) : this(outerName, outerNamespace, dataContractExtension, null) { } public SyndicationElementExtension(string outerName, string outerNamespace, object dataContractExtension, XmlObjectSerializer dataContractSerializer) { if (dataContractExtension == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dataContractExtension"); } if (outerName == string.Empty) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR2.GetString(SR2.JsonInvalidLocalNameEmpty)); } if (dataContractSerializer == null) { dataContractSerializer = new DataContractSerializer(dataContractExtension.GetType()); } this.outerName = outerName; this.outerNamespace = outerNamespace; this.extensionData = dataContractExtension; this.extensionDataWriter = new ExtensionDataWriter(this.extensionData, dataContractSerializer, this.outerName, this.outerNamespace); } public SyndicationElementExtension(object xmlSerializerExtension, XmlSerializer serializer) { if (xmlSerializerExtension == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("xmlSerializerExtension"); } if (serializer == null) { serializer = new XmlSerializer(xmlSerializerExtension.GetType()); } this.extensionData = xmlSerializerExtension; this.extensionDataWriter = new ExtensionDataWriter(this.extensionData, serializer); } internal SyndicationElementExtension(XmlBuffer buffer, int bufferElementIndex, string outerName, string outerNamespace) { this.buffer = buffer; this.bufferElementIndex = bufferElementIndex; this.outerName = outerName; this.outerNamespace = outerNamespace; } public string OuterName { get { if (this.outerName == null) { EnsureOuterNameAndNs(); } return this.outerName; } } public string OuterNamespace { get { if (this.outerName == null) { EnsureOuterNameAndNs(); } return this.outerNamespace; } } public TExtension GetObject() { return GetObject (new DataContractSerializer(typeof(TExtension))); } public TExtension GetObject (XmlObjectSerializer serializer) { if (serializer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer"); } if (this.extensionData != null && typeof(TExtension).IsAssignableFrom(extensionData.GetType())) { return (TExtension) this.extensionData; } using (XmlReader reader = GetReader()) { return (TExtension) serializer.ReadObject(reader, false); } } public TExtension GetObject (XmlSerializer serializer) { if (serializer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer"); } if (this.extensionData != null && typeof(TExtension).IsAssignableFrom(extensionData.GetType())) { return (TExtension) this.extensionData; } using (XmlReader reader = GetReader()) { return (TExtension) serializer.Deserialize(reader); } } public XmlReader GetReader() { this.EnsureBuffer(); XmlReader reader = this.buffer.GetReader(0); int index = 0; reader.ReadStartElement(Rss20Constants.ExtensionWrapperTag); while (reader.IsStartElement()) { if (index == this.bufferElementIndex) { break; } ++index; reader.Skip(); } return reader; } public void WriteTo(XmlWriter writer) { if (writer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer"); } if (this.extensionDataWriter != null) { this.extensionDataWriter.WriteTo(writer); } else { using (XmlReader reader = GetReader()) { writer.WriteNode(reader, false); } } } void EnsureBuffer() { if (this.buffer == null) { this.buffer = new XmlBuffer(int.MaxValue); using (XmlDictionaryWriter writer = this.buffer.OpenSection(XmlDictionaryReaderQuotas.Max)) { writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag); this.WriteTo(writer); writer.WriteEndElement(); } buffer.CloseSection(); buffer.Close(); this.bufferElementIndex = 0; } } void EnsureOuterNameAndNs() { Fx.Assert(this.extensionDataWriter != null, "outer name is null only for datacontract and xmlserializer cases"); this.extensionDataWriter.ComputeOuterNameAndNs(out this.outerName, out this.outerNamespace); } // this class holds the extension data and the associated serializer (either DataContractSerializer or XmlSerializer but not both) class ExtensionDataWriter { readonly XmlObjectSerializer dataContractSerializer; readonly object extensionData; readonly string outerName; readonly string outerNamespace; readonly XmlSerializer xmlSerializer; public ExtensionDataWriter(object extensionData, XmlObjectSerializer dataContractSerializer, string outerName, string outerNamespace) { Fx.Assert(extensionData != null && dataContractSerializer != null, "null check"); this.dataContractSerializer = dataContractSerializer; this.extensionData = extensionData; this.outerName = outerName; this.outerNamespace = outerNamespace; } public ExtensionDataWriter(object extensionData, XmlSerializer serializer) { Fx.Assert(extensionData != null && serializer != null, "null check"); this.xmlSerializer = serializer; this.extensionData = extensionData; } public void WriteTo(XmlWriter writer) { if (this.xmlSerializer != null) { Fx.Assert((this.dataContractSerializer == null && this.outerName == null && this.outerNamespace == null), "Xml serializer cannot have outer name, ns"); this.xmlSerializer.Serialize(writer, this.extensionData); } else { Fx.Assert(this.xmlSerializer == null, "Xml serializer cannot be configured"); if (this.outerName != null) { writer.WriteStartElement(outerName, outerNamespace); this.dataContractSerializer.WriteObjectContent(writer, this.extensionData); writer.WriteEndElement(); } else { this.dataContractSerializer.WriteObject(writer, this.extensionData); } } } internal void ComputeOuterNameAndNs(out string name, out string ns) { if (this.outerName != null) { Fx.Assert(this.xmlSerializer == null, "outer name is not null for data contract extension only"); name = this.outerName; ns = this.outerNamespace; } else if (this.dataContractSerializer != null) { Fx.Assert(this.xmlSerializer == null, "only one of xmlserializer or datacontract serializer can be present"); XsdDataContractExporter dcExporter = new XsdDataContractExporter(); XmlQualifiedName qName = dcExporter.GetRootElementName(this.extensionData.GetType()); if (qName != null) { name = qName.Name; ns = qName.Namespace; } else { // this can happen if an IXmlSerializable type is specified with IsAny=true ReadOuterNameAndNs(out name, out ns); } } else { Fx.Assert(this.dataContractSerializer == null, "only one of xmlserializer or datacontract serializer can be present"); XmlReflectionImporter importer = new XmlReflectionImporter(); XmlTypeMapping typeMapping = importer.ImportTypeMapping(this.extensionData.GetType()); if (typeMapping != null && !string.IsNullOrEmpty(typeMapping.ElementName)) { name = typeMapping.ElementName; ns = typeMapping.Namespace; } else { // this can happen if an IXmlSerializable type is specified with IsAny=true ReadOuterNameAndNs(out name, out ns); } } } internal void ReadOuterNameAndNs(out string name, out string ns) { using (MemoryStream stream = new MemoryStream()) { using (XmlWriter writer = XmlWriter.Create(stream)) { this.WriteTo(writer); } stream.Seek(0, SeekOrigin.Begin); using (XmlReader reader = XmlReader.Create(stream)) { SyndicationFeedFormatter.MoveToStartElement(reader); name = reader.LocalName; ns = reader.NamespaceURI; } } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FloatSumAggregationOperator.cs
- KnownBoxes.cs
- PinnedBufferMemoryStream.cs
- validationstate.cs
- StsCommunicationException.cs
- ExceptionList.cs
- XmlTextWriter.cs
- Evidence.cs
- DirectoryInfo.cs
- ParagraphVisual.cs
- ValidateNames.cs
- DataServiceClientException.cs
- ChangesetResponse.cs
- ErrorLog.cs
- MobileControlsSectionHelper.cs
- WSSecurityPolicy.cs
- RelationshipEndMember.cs
- XmlNamespaceMapping.cs
- ToolStripHighContrastRenderer.cs
- diagnosticsswitches.cs
- ServiceOperationUIEditor.cs
- NetworkCredential.cs
- TextRangeEditLists.cs
- XmlILConstructAnalyzer.cs
- AssociationEndMember.cs
- CqlLexerHelpers.cs
- HttpAsyncResult.cs
- WebControlParameterProxy.cs
- DataGridViewCellValueEventArgs.cs
- TextEditor.cs
- LayeredChannelFactory.cs
- HostedHttpContext.cs
- MessageParameterAttribute.cs
- CustomTypeDescriptor.cs
- UrlSyndicationContent.cs
- XmlStringTable.cs
- XmlName.cs
- ButtonRenderer.cs
- HttpCachePolicyElement.cs
- CodeArrayCreateExpression.cs
- ADMembershipUser.cs
- wgx_exports.cs
- EntityCommandCompilationException.cs
- xamlnodes.cs
- CodeArrayCreateExpression.cs
- MatchAttribute.cs
- Italic.cs
- MatrixAnimationUsingKeyFrames.cs
- KeyTime.cs
- Html32TextWriter.cs
- PageVisual.cs
- DefaultParameterValueAttribute.cs
- ObjectDataSourceEventArgs.cs
- PriorityChain.cs
- TraceLog.cs
- MSHTMLHostUtil.cs
- SmtpFailedRecipientsException.cs
- HybridWebProxyFinder.cs
- ItemChangedEventArgs.cs
- DataAccessor.cs
- SqlRecordBuffer.cs
- Decorator.cs
- OpenTypeCommon.cs
- StrokeSerializer.cs
- LinearKeyFrames.cs
- DecoratedNameAttribute.cs
- ClientConfigurationSystem.cs
- AssemblyNameProxy.cs
- KeyProperty.cs
- TempFiles.cs
- WorkflowTransactionService.cs
- SemanticTag.cs
- ResourceExpressionBuilder.cs
- CorePropertiesFilter.cs
- Soap12ServerProtocol.cs
- LocalizationParserHooks.cs
- HexParser.cs
- ProviderConnectionPoint.cs
- SingleObjectCollection.cs
- BinaryObjectWriter.cs
- EndEvent.cs
- Win32.cs
- FullTextLine.cs
- SQLGuid.cs
- StoreItemCollection.Loader.cs
- ProgressiveCrcCalculatingStream.cs
- DataSvcMapFile.cs
- httpapplicationstate.cs
- ComponentDispatcherThread.cs
- OracleDateTime.cs
- ObjectResult.cs
- HtmlTableCellCollection.cs
- RepeaterCommandEventArgs.cs
- GPPOINT.cs
- XsdValidatingReader.cs
- UrlMappingsModule.cs
- TdsParserStateObject.cs
- Literal.cs
- WebException.cs
- ToolStripContainer.cs