本文整理汇总了C#中System.Xml.Linq.XAttribute.SetValue方法的典型用法代码示例。如果您正苦于以下问题:C# XAttribute.SetValue方法的具体用法?C# XAttribute.SetValue怎么用?C# XAttribute.SetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XAttribute
的用法示例。
在下文中一共展示了XAttribute.SetValue方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteXAttributeVariation
public void ExecuteXAttributeVariation(XAttribute toChange, object newValue)
{
XElement xElem = new XElement("root", toChange);
XElement xElemOriginal = new XElement(xElem);
using (UndoManager undo = new UndoManager(xElem))
{
undo.Group();
using (EventsHelper eHelper = new EventsHelper(toChange))
{
toChange.SetValue(newValue);
Assert.True(newValue.Equals(toChange.Value), "Value did not change");
xElem.Verify();
eHelper.Verify(XObjectChange.Value, toChange);
}
undo.Undo();
Assert.True(xElem.Nodes().SequenceEqual(xElemOriginal.Nodes(), XNode.EqualityComparer), "Undo did not work!");
Assert.True(xElem.Attributes().EqualsAllAttributes(xElemOriginal.Attributes(), Helpers.MyAttributeComparer), "Undo did not work!");
}
}
示例2: CopyData
private static void CopyData(string providerName, DataTypeChangeDescriptor dataTypeChangeDescriptor, DataScopeConfigurationElement oldDataScopeConfigurationElement, DataScopeConfigurationElement newDataScopeConfigurationElement, Dictionary<string, object> newFieldValues, bool deleteOldFile = true)
{
string oldFilename = ResolvePath(oldDataScopeConfigurationElement.Filename, providerName);
string newFilename = ResolvePath(newDataScopeConfigurationElement.Filename, providerName);
XDocument oldDocument = XDocumentUtils.Load(PathUtil.Resolve(oldFilename));
List<XElement> newElements = new List<XElement>();
foreach (XElement oldElement in oldDocument.Root.Elements())
{
List<XAttribute> newChildAttributes = new List<XAttribute>();
foreach (XAttribute oldChildAttribute in oldElement.Attributes())
{
var existingFieldInfo = GetExistingFieldInfo(dataTypeChangeDescriptor, oldChildAttribute.Name.LocalName);
if (existingFieldInfo != null)
{
if (existingFieldInfo.OriginalField.Name != existingFieldInfo.AlteredField.Name)
{
XAttribute newChildAttribute = new XAttribute(existingFieldInfo.AlteredField.Name, oldChildAttribute.Value);
newChildAttributes.Add(newChildAttribute);
}
else
{
newChildAttributes.Add(oldChildAttribute);
}
}
// It may happen that some data were added before data descriptors are updated, in the case of using
// [AutoUpdateable] attribute.
else if (dataTypeChangeDescriptor.AddedFields.Any(addedField => addedField.Name == oldChildAttribute.Name))
{
newChildAttributes.Add(oldChildAttribute);
}
}
// Adding default value for fields that are NULL and become required
foreach (var existingFieldInfo in dataTypeChangeDescriptor.ExistingFields)
{
bool fieldBecomeRequired = existingFieldInfo.OriginalField.IsNullable && !existingFieldInfo.AlteredField.IsNullable;
string fieldName = existingFieldInfo.AlteredField.Name;
if (fieldBecomeRequired && !newChildAttributes.Any(attr => attr.Name.LocalName == fieldName))
{
newChildAttributes.Add(new XAttribute(fieldName, GetDefaultValue(existingFieldInfo.AlteredField)));
}
}
foreach (DataFieldDescriptor fieldDescriptor in dataTypeChangeDescriptor.AddedFields)
{
if (fieldDescriptor.IsNullable == false
&& !newChildAttributes.Any(attr => attr.Name == fieldDescriptor.Name))
{
XAttribute newChildAttribute = new XAttribute(fieldDescriptor.Name, GetDefaultValue(fieldDescriptor));
if (newFieldValues.ContainsKey(fieldDescriptor.Name))
{
newChildAttribute.SetValue(newFieldValues[fieldDescriptor.Name]);
}
newChildAttributes.Add(newChildAttribute);
}
else if (newFieldValues.ContainsKey(fieldDescriptor.Name))
{
XAttribute attribute = newChildAttributes.SingleOrDefault(attr => attr.Name == fieldDescriptor.Name);
if (attribute != null)
{
attribute.SetValue(newFieldValues[fieldDescriptor.Name]);
}
}
}
XElement newElement = new XElement(newDataScopeConfigurationElement.ElementName, newChildAttributes);
newElements.Add(newElement);
}
if (deleteOldFile)
{
C1File.Delete(oldFilename);
}
XElement newRoot = new XElement(XmlDataProviderDocumentWriter.GetRootElementName(newDataScopeConfigurationElement.ElementName));
newRoot.Add(newElements);
XDocument newDocument = new XDocument();
newDocument.Add(newRoot);
XDocumentUtils.Save(newDocument, newFilename);
}
示例3: SetValueNullAttr
//[Variation(Desc = "XAttribute.SetValue(null)")]
public void SetValueNullAttr()
{
XAttribute a = new XAttribute("a", "A");
try
{
a.SetValue(null);
throw new TestException(TestResult.Failed, "");
}
catch (ArgumentNullException) { }
}
示例4: CreateContainer
private XObject CreateContainer()
{
if (_nodeCreateType == NodeCreateType.Constructor)
{
if (_nodeType.Name.Equals("XElement"))
{
return new XElement(XName.Get("name"), _data);
}
if (_nodeType.Name.Equals("XAttribute"))
{
return new XAttribute(XName.Get("name"), _data);
}
}
else if (_nodeCreateType == NodeCreateType.SetValue)
{
if (_nodeType.Name.Equals("XElement"))
{
var node = new XElement(XName.Get("name"), "");
node.SetValue(_data);
return node;
}
if (_nodeType.Name.Equals("XAttribute"))
{
var node = new XAttribute(XName.Get("name"), "");
node.SetValue(_data);
return node;
}
}
throw new ArgumentOutOfRangeException("Unknown NodeCreateType: " + _nodeCreateType);
}
示例5: ChangeVersionInfo
private static void ChangeVersionInfo( XAttribute t )
{
Group version = Regex.Match( t.Value, "Version=(?<1>[\\d\\.]+)" ).Groups[1];
var current = new Version( version.Value );
var next = new Version( current.Major + 1, current.Minor + 1, current.Build, current.Revision );
string newTypeName = t.Value.Remove( version.Index, version.Length ).Insert( version.Index, next.ToString() );
t.SetValue( newTypeName );
}