Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / XPathQueryGenerator.cs / 1305376 / XPathQueryGenerator.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Runtime.Serialization
{
using System;
using System.Text;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using System.Xml;
public static class XPathQueryGenerator
{
const string XPathSeparator = "/";
const string NsSeparator = ":";
public static string CreateFromDataContractSerializer(Type type, MemberInfo[] pathToMember, out XmlNamespaceManager namespaces)
{
return CreateFromDataContractSerializer(type, pathToMember, null, out namespaces);
}
// Here you can provide your own root element Xpath which will replace the Xpath of the top level element
public static string CreateFromDataContractSerializer(Type type, MemberInfo[] pathToMember, StringBuilder rootElementXpath, out XmlNamespaceManager namespaces)
{
if (type == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
}
if (pathToMember == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("pathToMember"));
}
DataContract currentContract = DataContract.GetDataContract(type);
ExportContext context;
if (rootElementXpath == null)
{
context = new ExportContext(currentContract);
}
else
{
// use the provided xpath for top level element
context = new ExportContext(rootElementXpath);
}
for (int pathToMemberIndex = 0; pathToMemberIndex < pathToMember.Length; pathToMemberIndex++)
{
currentContract = ProcessDataContract(currentContract, context, pathToMember[pathToMemberIndex]);
}
namespaces = context.Namespaces;
return context.XPath;
}
static DataContract ProcessDataContract(DataContract contract, ExportContext context, MemberInfo memberNode)
{
if (contract is ClassDataContract)
{
return ProcessClassDataContract((ClassDataContract)contract, context, memberNode);
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.QueryGeneratorPathToMemberNotFound)));
}
static DataContract ProcessClassDataContract(ClassDataContract contract, ExportContext context, MemberInfo memberNode)
{
string prefix = context.SetNamespace(contract.Namespace.Value);
if (contract.Members != null)
{
foreach (DataMember member in contract.Members)
{
if (member.MemberInfo == memberNode)
{
context.WriteChildToContext(member, prefix);
return member.MemberTypeContract;
}
}
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.QueryGeneratorPathToMemberNotFound)));
}
class ExportContext
{
XmlNamespaceManager namespaces;
int nextPrefix;
StringBuilder xPathBuilder;
public ExportContext(DataContract rootContract)
{
this.namespaces = new XmlNamespaceManager(new NameTable());
string prefix = SetNamespace(rootContract.TopLevelElementNamespace.Value);
this.xPathBuilder = new StringBuilder(XPathQueryGenerator.XPathSeparator + prefix + XPathQueryGenerator.NsSeparator + rootContract.TopLevelElementName.Value);
}
public ExportContext(StringBuilder rootContractXPath)
{
this.namespaces = new XmlNamespaceManager(new NameTable());
this.xPathBuilder = rootContractXPath;
}
public void WriteChildToContext(DataMember contextMember, string prefix)
{
this.xPathBuilder.Append(XPathQueryGenerator.XPathSeparator + prefix + XPathQueryGenerator.NsSeparator + contextMember.Name);
}
public XmlNamespaceManager Namespaces
{
get
{
return this.namespaces;
}
}
public string XPath
{
get
{
return this.xPathBuilder.ToString();
}
}
public string SetNamespace(string ns)
{
string prefix = namespaces.LookupPrefix(ns);
if (prefix == null || prefix.Length == 0)
{
prefix = "xg" + (this.nextPrefix++).ToString(NumberFormatInfo.InvariantInfo);
Namespaces.AddNamespace(prefix, ns);
}
return prefix;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Runtime.Serialization
{
using System;
using System.Text;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using System.Xml;
public static class XPathQueryGenerator
{
const string XPathSeparator = "/";
const string NsSeparator = ":";
public static string CreateFromDataContractSerializer(Type type, MemberInfo[] pathToMember, out XmlNamespaceManager namespaces)
{
return CreateFromDataContractSerializer(type, pathToMember, null, out namespaces);
}
// Here you can provide your own root element Xpath which will replace the Xpath of the top level element
public static string CreateFromDataContractSerializer(Type type, MemberInfo[] pathToMember, StringBuilder rootElementXpath, out XmlNamespaceManager namespaces)
{
if (type == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
}
if (pathToMember == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("pathToMember"));
}
DataContract currentContract = DataContract.GetDataContract(type);
ExportContext context;
if (rootElementXpath == null)
{
context = new ExportContext(currentContract);
}
else
{
// use the provided xpath for top level element
context = new ExportContext(rootElementXpath);
}
for (int pathToMemberIndex = 0; pathToMemberIndex < pathToMember.Length; pathToMemberIndex++)
{
currentContract = ProcessDataContract(currentContract, context, pathToMember[pathToMemberIndex]);
}
namespaces = context.Namespaces;
return context.XPath;
}
static DataContract ProcessDataContract(DataContract contract, ExportContext context, MemberInfo memberNode)
{
if (contract is ClassDataContract)
{
return ProcessClassDataContract((ClassDataContract)contract, context, memberNode);
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.QueryGeneratorPathToMemberNotFound)));
}
static DataContract ProcessClassDataContract(ClassDataContract contract, ExportContext context, MemberInfo memberNode)
{
string prefix = context.SetNamespace(contract.Namespace.Value);
if (contract.Members != null)
{
foreach (DataMember member in contract.Members)
{
if (member.MemberInfo == memberNode)
{
context.WriteChildToContext(member, prefix);
return member.MemberTypeContract;
}
}
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.QueryGeneratorPathToMemberNotFound)));
}
class ExportContext
{
XmlNamespaceManager namespaces;
int nextPrefix;
StringBuilder xPathBuilder;
public ExportContext(DataContract rootContract)
{
this.namespaces = new XmlNamespaceManager(new NameTable());
string prefix = SetNamespace(rootContract.TopLevelElementNamespace.Value);
this.xPathBuilder = new StringBuilder(XPathQueryGenerator.XPathSeparator + prefix + XPathQueryGenerator.NsSeparator + rootContract.TopLevelElementName.Value);
}
public ExportContext(StringBuilder rootContractXPath)
{
this.namespaces = new XmlNamespaceManager(new NameTable());
this.xPathBuilder = rootContractXPath;
}
public void WriteChildToContext(DataMember contextMember, string prefix)
{
this.xPathBuilder.Append(XPathQueryGenerator.XPathSeparator + prefix + XPathQueryGenerator.NsSeparator + contextMember.Name);
}
public XmlNamespaceManager Namespaces
{
get
{
return this.namespaces;
}
}
public string XPath
{
get
{
return this.xPathBuilder.ToString();
}
}
public string SetNamespace(string ns)
{
string prefix = namespaces.LookupPrefix(ns);
if (prefix == null || prefix.Length == 0)
{
prefix = "xg" + (this.nextPrefix++).ToString(NumberFormatInfo.InvariantInfo);
Namespaces.AddNamespace(prefix, ns);
}
return prefix;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- srgsitem.cs
- LicenseContext.cs
- EntityProviderServices.cs
- EntityDataSourceColumn.cs
- XmlSerializationWriter.cs
- ColumnMap.cs
- CreateUserErrorEventArgs.cs
- StickyNote.cs
- IdentityHolder.cs
- SecurityContext.cs
- RequestBringIntoViewEventArgs.cs
- IsolatedStorageFile.cs
- ReceiveActivityValidator.cs
- ToolStripContainerActionList.cs
- DataServiceClientException.cs
- InstanceKeyNotReadyException.cs
- QueueException.cs
- GraphicsPath.cs
- Relationship.cs
- DataGridViewDataErrorEventArgs.cs
- TypedReference.cs
- WebContentFormatHelper.cs
- EndPoint.cs
- SmiMetaData.cs
- PhonemeConverter.cs
- GridView.cs
- BaseValidatorDesigner.cs
- SendKeys.cs
- DbConnectionPool.cs
- DataRowChangeEvent.cs
- ToolStripContentPanelDesigner.cs
- TemplateColumn.cs
- SystemFonts.cs
- ServiceBusyException.cs
- XpsDigitalSignature.cs
- ExtensionQuery.cs
- Marshal.cs
- ExclusiveCanonicalizationTransform.cs
- DelegatedStream.cs
- EntityContainerEmitter.cs
- MemberDescriptor.cs
- TrackPoint.cs
- TextEffectResolver.cs
- Tuple.cs
- SiteMapDataSource.cs
- PermissionSetEnumerator.cs
- ParenthesizePropertyNameAttribute.cs
- HelpProvider.cs
- ArgumentValueSerializer.cs
- StorageMappingItemCollection.cs
- PlanCompilerUtil.cs
- ConstNode.cs
- ContractCodeDomInfo.cs
- ExpressionCopier.cs
- FileUtil.cs
- PcmConverter.cs
- DesignerDataView.cs
- ConvertEvent.cs
- LambdaCompiler.Lambda.cs
- InternalSafeNativeMethods.cs
- Membership.cs
- ContentWrapperAttribute.cs
- ControlDesigner.cs
- XPathDocumentNavigator.cs
- PersonalizationProviderHelper.cs
- SspiWrapper.cs
- xmlNames.cs
- Schema.cs
- EventTrigger.cs
- DrawingContext.cs
- PieceNameHelper.cs
- TextTreeRootNode.cs
- KeyFrames.cs
- SpanIndex.cs
- ReferenceConverter.cs
- AuthenticationService.cs
- OrderByExpression.cs
- ServiceHostingEnvironment.cs
- sqlnorm.cs
- ContextBase.cs
- MaterialGroup.cs
- SoapReflectionImporter.cs
- InstanceDataCollection.cs
- SourceSwitch.cs
- TextContainerChangedEventArgs.cs
- DependencyPropertyKind.cs
- RegexCode.cs
- DiscoveryClientReferences.cs
- VisualBrush.cs
- StringResourceManager.cs
- SchemaMerger.cs
- DefaultParameterValueAttribute.cs
- DesignerDataStoredProcedure.cs
- GPPOINTF.cs
- PropertyFilterAttribute.cs
- PeerNameResolver.cs
- SQLBinaryStorage.cs
- AssemblySettingAttributes.cs
- GlobalProxySelection.cs
- DataTableClearEvent.cs