本文整理汇总了C#中System.Xml.Linq.XDocument.GetOrCreateElement方法的典型用法代码示例。如果您正苦于以下问题:C# XDocument.GetOrCreateElement方法的具体用法?C# XDocument.GetOrCreateElement怎么用?C# XDocument.GetOrCreateElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XDocument
的用法示例。
在下文中一共展示了XDocument.GetOrCreateElement方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOrCreateRootElem
private XElement GetOrCreateRootElem(XDocument document, string rootElementName)
{
var rootElement = document.Element(rootElementName);
if (rootElement == null)
{
if (document.Root != null)
rootElement = document.Root.GetOrCreateElement(rootElementName);
else
rootElement = document.GetOrCreateElement(rootElementName);
}
return rootElement;
}
示例2: AddConnectionFactoryToConfig
// <summary>
// Checks whether or not the given XML document representing a .config file contains
// an EntityFramework "defaultConnectionFactory" entry or not. If no entry is found then one
// is added for the given connection factory specification.
// </summary>
// <param name="config"> An XML document representing the config file. </param>
// <param name="specification"> Specifies the connection factory and constructor arguments to use. </param>
// <returns> True if the document was modified; false if no change was made. </returns>
public virtual bool AddConnectionFactoryToConfig(XDocument config, ConnectionFactorySpecification specification)
{
DebugCheck.NotNull(config);
DebugCheck.NotNull(specification);
var entityFramework = config
.GetOrCreateElement(ConfigurationElementName)
.GetOrCreateElement(EntityFrameworkElementName);
if (entityFramework.Elements(DefaultConnectionFactoryElementName).Any())
{
return false;
}
var factoryElement = entityFramework
.GetOrCreateElement(
DefaultConnectionFactoryElementName,
new XAttribute("type", specification.ConnectionFactoryName));
AddFactoryArguments(factoryElement, specification);
return true;
}
示例3: AddOrUpdateConnectionFactoryInConfig
/// <summary>
/// Sets the EntityFramework "defaultConnectionFactory" in the given XML document representing a
/// .config file to use th given specification. This method differs from AddConnectionFactoryToConfig
/// in that it always sets the entry to use the given specification even if it was already present
/// and set to something else.
/// </summary>
/// <param name="config"> An XML document representing the config file.</param>
/// <param name="specification"> Specifies the connection factory and constructor arguments to use.</param>
/// <returns>True if the document was modified; false if no change was made.</returns>
public virtual bool AddOrUpdateConnectionFactoryInConfig(XDocument config, ConnectionFactorySpecification specification)
{
Contract.Requires(config != null);
var connectionFactoryElement = config
.GetOrCreateElement(ConfigurationElementName)
.GetOrCreateElement(EntityFrameworkElementName)
.GetOrCreateElement(DefaultConnectionFactoryElementName);
var currentFactoryAttribute = connectionFactoryElement.Attribute("type");
if (currentFactoryAttribute != null
&& specification.ConnectionFactoryName.Equals(currentFactoryAttribute.Value, StringComparison.OrdinalIgnoreCase))
{
return false;
}
connectionFactoryElement.RemoveAll();
connectionFactoryElement.Add(new XAttribute("type", specification.ConnectionFactoryName));
AddFactoryArguments(connectionFactoryElement, specification);
return true;
}
示例4: AddOrUpdateConfigSection
// <summary>
// Ensures that the config file has a defined "entityFramework" section and that it references
// the current version of the EntityFramework.dll assembly.
// </summary>
// <param name="config"> An XML document representing the config file. </param>
// <param name="entityFrameworkVersion"> The version of EntityFramework.dll to use. </param>
// <returns> True if the document was modified; false if no change was made. </returns>
public virtual bool AddOrUpdateConfigSection(XDocument config, Version entityFrameworkVersion)
{
DebugCheck.NotNull(config);
DebugCheck.NotNull(entityFrameworkVersion);
var configSections = config
.GetOrCreateElement(ConfigurationElementName)
.GetOrCreateElement(ConfigSectionsElementName);
var typeAttribute = configSections
.Elements(SectionElementName)
.Where(e => (string)e.Attribute("name") == EntityFrameworkElementName)
.SelectMany(e => e.Attributes("type"))
.FirstOrDefault();
// Hard coding this so that we don't need to load EntityFramework.dll to get it.
const string entityFrameworkSectionName =
"System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
var efSectionTypeName = string.Format(
CultureInfo.InvariantCulture, entityFrameworkSectionName,
entityFrameworkVersion);
if (typeAttribute != null)
{
var sectionElement = typeAttribute.Parent;
var requirePermissionAttribute = sectionElement.Attribute("requirePermission");
if (efSectionTypeName.Equals(typeAttribute.Value, StringComparison.InvariantCultureIgnoreCase)
&& requirePermissionAttribute != null)
{
return false;
}
typeAttribute.Value = efSectionTypeName;
if (requirePermissionAttribute == null)
{
sectionElement.Add(new XAttribute("requirePermission", false));
}
}
else
{
configSections.Add(
new XElement(
SectionElementName, new XAttribute("name", EntityFrameworkElementName),
new XAttribute("type", efSectionTypeName), new XAttribute("requirePermission", false)));
}
return true;
}
示例5: AddProviderToConfig
public virtual bool AddProviderToConfig(XDocument config, string invariantName, string typeName)
{
DebugCheck.NotNull(config);
var providersElement = config
.GetOrCreateElement(ConfigurationElementName)
.GetOrCreateElement(EntityFrameworkElementName)
.GetOrCreateElement(ProvidersElementName);
var invariantAttribute = new XAttribute("invariantName", invariantName);
var modificationMade = false;
// Check if element exists at end
var providerElement = providersElement.Elements(ProviderElementName).LastOrDefault();
if (providerElement == null
|| providerElement.Attributes(invariantAttribute.Name).All(a => a.Value != invariantAttribute.Value))
{
// Check if element exists and if so move it to end
providerElement = providersElement
.Elements(ProviderElementName)
.FirstOrDefault(e => e.Attributes(invariantAttribute.Name).Any(a => a.Value == invariantAttribute.Value));
if (providerElement != null)
{
providerElement.Remove();
}
else
{
providerElement = new XElement(ProviderElementName, invariantAttribute);
}
providersElement.Add(providerElement);
modificationMade = true;
}
var currentTypeAttribute = providerElement.Attribute("type");
if (currentTypeAttribute == null)
{
providerElement.Add(new XAttribute("type", typeName));
modificationMade = true;
}
else if (!typeName.Equals(currentTypeAttribute.Value, StringComparison.OrdinalIgnoreCase))
{
currentTypeAttribute.Value = typeName;
modificationMade = true;
}
return modificationMade;
}