static void MoveAttributeIfAny(System.Xml.Linq.XName attributeName, System.Xml.Linq.XElement from, System.Xml.Linq.XElement to) { System.Xml.Linq.XAttribute attr = from.Attribute(attributeName); if (attr != null) { // Remove the attribute from its current parent: attr.Remove(); // Add the attribute to its new parent to.Add(attr); } }
/// <summary> /// Changes the prefix or URI of a namespace declaration. This is non-trivial since XAttribute does not support this by default. /// </summary> /// <param name="doc"></param> /// <param name="oldDec"></param> /// <param name="parrent"></param> /// <param name="newUri"></param> /// <param name="newPrefix"></param> /// <returns></returns> public XAttribute ChangeNamespaceDeclaration(XDocument doc, XAttribute oldDec, XElement parrent, string newUri, string newPrefix) { ChangeNamespace(oldDec.Name.LocalName, oldDec.Value, newPrefix, newUri); string oldNamespace = oldDec.Value; oldDec.Remove(); newPrefix = newPrefix.Replace("xmlns:", ""); XAttribute newAttribute = new XAttribute(XNamespace.Xmlns + newPrefix, newUri); parrent.Add(newAttribute); foreach (XElement element in doc.Elements()) { ChangeNamespaceForElement(element, oldNamespace, newUri); } return newAttribute; }
/// <summary> /// Removes each <see cref="XAttribute"/> represented in this <see cref="IEnumerable"/> of /// <see cref="XAttribute"/>. Note that this method uses snapshot semantics (copies the /// attributes to an array before deleting each). /// </summary> public static void Remove(this IEnumerable <XAttribute> source) { if (source == null) { throw new ArgumentNullException("source"); } int count; XAttribute[] attributes = EnumerableHelpers.ToArray(source, out count); for (int i = 0; i < count; i++) { XAttribute a = attributes[i]; if (a != null) { a.Remove(); } } }
public void SetAttributeValue(XName name, object value) { XAttribute a = Attribute(name); if (value == null) { if (a != null) { a.Remove(); } } else { if (a == null) { SetAttributeObject(new XAttribute(name, value)); } else { a.Value = XUtil.ToString(value); } } }
public void OwningSequencePropertyHasCorrectResponses() { var element = new XElement("CmPossibility", new XAttribute(SharedConstants.GuidStr, "c1ee310c-e382-11de-8a39-0800200c9a66")); AddBasicPropertyElementsToPoss(element); var prop = new XElement("SubPossibilities"); element.Add(prop); var extraAttr = new XAttribute("bogus", "badvalue"); prop.Add(extraAttr); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); extraAttr.Remove(); // No children is fine. result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); var extraChild = new XElement("BogusChild"); prop.Add(extraChild); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Contains unrecognized child elements")); extraChild.Remove(); }
public void OwningCollectionPropertyHasCorrectResponses() { var element = new XElement("StText", new XAttribute(SharedConstants.GuidStr, "c1ee310d-e382-11de-8a39-0800200c9a66"), new XElement("DateModified", new XAttribute(SharedConstants.Val, "2013-1-1 19:39:28.829")), new XElement("RightToLeft", new XAttribute(SharedConstants.Val, "True"))); var prop = new XElement("Tags"); element.Add(prop); // Owns col of TextTag var extraAttr = new XAttribute("bogus", "badvalue"); prop.Add(extraAttr); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); extraAttr.Remove(); // No children is fine. result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); var ttElement1 = new XElement("TextTag", new XAttribute(SharedConstants.GuidStr, "c1ee310e-e382-11de-8a39-0800200c9a66"), new XElement("BeginAnalysisIndex", new XAttribute(SharedConstants.Val, 1)), new XElement("EndAnalysisIndex", new XAttribute(SharedConstants.Val, 1))); var ttElement2 = new XElement("TextTag", new XAttribute(SharedConstants.GuidStr, "c1ee310f-e382-11de-8a39-0800200c9a66"), new XElement("BeginAnalysisIndex", new XAttribute(SharedConstants.Val, 1)), new XElement("EndAnalysisIndex", new XAttribute(SharedConstants.Val, 1))); prop.Add(ttElement1); prop.Add(ttElement2); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); }
public void ReferenceCollectionPropertyHasCorrectResponses() { var element = new XElement("CmPossibility", new XAttribute(SharedConstants.GuidStr, "c1ee3106-e382-11de-8a39-0800200c9a66")); AddBasicPropertyElementsToPoss(element); var prop = new XElement("Restrictions"); element.Add(prop); var extraAttr = new XAttribute("bogus", "badvalue"); prop.Add(extraAttr); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); extraAttr.Remove(); var extraChild = new XElement("BogusChild"); prop.Add(extraChild); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Contains child elements that are not 'refcol'")); extraChild.Remove(); var refcol1 = new XElement(SharedConstants.Refcol, new XAttribute(SharedConstants.GuidStr, "c1ee3107-e382-11de-8a39-0800200c9a66"), new XAttribute("t", "r")); var refcol2 = new XElement(SharedConstants.Refcol, new XAttribute(SharedConstants.GuidStr, "c1ee3108-e382-11de-8a39-0800200c9a66"), new XAttribute("t", "r")); prop.Add(refcol1); prop.Add(refcol2); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); }
public void OwningAtomicPropertyHasCorrectResponses() { var element = new XElement("CmPossibility", new XAttribute(SharedConstants.GuidStr, "c1ee3109-e382-11de-8a39-0800200c9a66")); AddBasicPropertyElementsToPoss(element); var prop = new XElement("Discussion"); element.Add(prop); var extraAttr = new XAttribute("bogus", "badvalue"); prop.Add(extraAttr); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); extraAttr.Remove(); var stText1 = new XElement("StText", new XAttribute(SharedConstants.GuidStr, "c1ee310a-e382-11de-8a39-0800200c9a66"), new XElement("DateModified", new XAttribute(SharedConstants.Val, "2013-1-1 19:39:28.829")), new XElement("RightToLeft", new XAttribute(SharedConstants.Val, "True"))); prop.Add(stText1); var stText2 = new XElement("StText", new XAttribute(SharedConstants.GuidStr, "c1ee310b-e382-11de-8a39-0800200c9a66"), new XElement("DateModified", new XAttribute(SharedConstants.Val, "2013-1-1 19:39:28.829")), new XElement("RightToLeft", new XAttribute(SharedConstants.Val, "True"))); prop.Add(stText2); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has too many child elements")); stText2.Remove(); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); stText1.Attribute(SharedConstants.GuidStr).Remove(); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("No guid attribute")); }
public void ReferenceAtomicPropertyHasCorrectResponses() { var element = new XElement("CmPossibility", new XAttribute(SharedConstants.GuidStr, "c1ee3102-e382-11de-8a39-0800200c9a66")); AddBasicPropertyElementsToPoss(element); var prop = new XElement("Confidence"); element.Add(prop); var objsurElement = new XElement(SharedConstants.Objsur); prop.Add(objsurElement); const string guidValue = "c1ee3113-e382-11de-8a39-0800200c9a66"; var guidAttr = new XAttribute(SharedConstants.GuidStr, guidValue); var typeAttr = new XAttribute("t", "r"); objsurElement.Add(guidAttr); objsurElement.Add(typeAttr); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); var extraAttr = new XAttribute("bogus", "badvalue"); prop.Add(extraAttr); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); extraAttr.Remove(); objsurElement.Add(extraAttr); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has too many attributes")); extraAttr.Remove(); var extraChild = new XElement("BogusChild"); objsurElement.Add(extraChild); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("'objsur' element has child element(s)")); extraChild.Remove(); prop.Add(extraChild); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has too many child elements")); extraChild.Remove(); guidAttr.Value = "badValue"; result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); guidAttr.Value = guidValue; guidAttr.Remove(); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); objsurElement.Add(guidAttr); typeAttr.Value = "o"; result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); typeAttr.Value = "r"; typeAttr.Remove(); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); }
public void ReferenceSequencePropertyHasCorrectResponses() { var element = new XElement("Segment", new XAttribute(SharedConstants.GuidStr, "c1ee3103-e382-11de-8a39-0800200c9a66"), new XElement("BeginOffset", new XAttribute(SharedConstants.Val, 1))); var prop = new XElement("Analyses"); element.Add(prop); var extraAttr = new XAttribute("bogus", "badvalue"); prop.Add(extraAttr); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); extraAttr.Remove(); var extraChild = new XElement("BogusChild"); prop.Add(extraChild); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Contains child elements that are not 'refseq'")); extraChild.Remove(); var refseq1 = new XElement(SharedConstants.Refseq, new XAttribute(SharedConstants.GuidStr, "c1ee3104-e382-11de-8a39-0800200c9a66"), new XAttribute("t", "r")); var refseq2 = new XElement(SharedConstants.Refseq, new XAttribute(SharedConstants.GuidStr, "c1ee3105-e382-11de-8a39-0800200c9a66"), new XAttribute("t", "r")); prop.Add(refseq1); prop.Add(refseq2); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); }
public void UnicodePropertyHasCorrectResponses() { var element = new XElement("CmFilter", new XAttribute(SharedConstants.GuidStr, "c1ee3101-e382-11de-8a39-0800200c9a66")); var prop = new XElement("Name"); var attr = new XAttribute(SharedConstants.Val, "badvalue"); prop.Add(attr); element.Add(prop); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); attr.Remove(); var extraElement = new XElement("badchild"); prop.Add(extraElement); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Unexpected child element")); extraElement.Remove(); var uniElement = new XElement(SharedConstants.Uni); prop.Add(uniElement); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); uniElement.Value = "SomeText."; result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); uniElement.Add(attr); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); attr.Remove(); uniElement.Add(extraElement); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has non-text child element")); extraElement.Remove(); var extraUniElement = new XElement(SharedConstants.Uni); prop.Add(extraUniElement); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Too many child elements")); extraUniElement.Remove(); }
public void XElementChangeAttributesParentInThePreEventHandler() { bool firstTime = true; XElement element = XElement.Parse("<root></root>"); XAttribute child = new XAttribute("Add", "Me"); element.Add(child); element.Changing += new EventHandler<XObjectChangeEventArgs>( delegate (object sender, XObjectChangeEventArgs e) { if (firstTime) { firstTime = false; child.Remove(); } }); Assert.Throws<InvalidOperationException>(() => { child.Remove(); }); element.Verify(); }
public void AttributeRemove() { XElement e = new XElement("element"); XAttribute a = new XAttribute("attribute", "value"); // Can't remove when no parent. Assert.Throws<InvalidOperationException>(() => a.Remove()); e.Add(a); Assert.Equal(1, e.Attributes().Count()); a.Remove(); Assert.Empty(e.Attributes()); }
/// <summary> /// Take an attribute that fukctions as a namespace declaration, and makes it not be a namespace declaration /// </summary> /// <param name="attribute"></param> /// <param name="parent"></param> /// <returns></returns> public XAttribute MakeAttributeNotBeNamespaceDeclaration(XAttribute attribute, XElement parent) { RemoveNamespace(attribute, parent); attribute.Remove(); XAttribute newAttribute = new XAttribute(attribute.Name.LocalName, attribute.Value); XmlHelper.AddAttributeToElement(newAttribute, parent); return newAttribute; }
/// <summary> /// Validates the behavior of the Remove method on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeRemove")] public void AttributeRemove() { XElement e = new XElement("element"); XAttribute a = new XAttribute("attribute", "value"); // Can't remove when no parent. try { a.Remove(); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } e.Add(a); Validate.Count(e.Attributes(), 1); a.Remove(); Validate.Count(e.Attributes(), 0); }
private static void FixUpEntitySetMapping( XAttribute typeNameAttribute, XElement entitySetMappingNode) { XName xn = XName.Get("EntityTypeMapping", entitySetMappingNode.Name.NamespaceName); typeNameAttribute.Remove(); XElement etm = new XElement(xn); etm.Add(typeNameAttribute); // move the "storeEntitySet" attribute into the new // EntityTypeMapping node foreach (XAttribute a in entitySetMappingNode.Attributes()) { if (a.Name.LocalName == "StoreEntitySet") { a.Remove(); etm.Add(a); break; } } // now move all descendants into this node ReparentChildren(entitySetMappingNode, etm); entitySetMappingNode.Add(etm); }
public new CSSStyleRuleMonkier this[Task task] { // x:\jsc.svn\examples\javascript\async\asyncworkersourcesha1\asyncworkersourcesha1\application.cs get { //var TaskIdentity = new Random().Next(); var TaskName = "incomplete"; if (InternalTaskNameLookup.ContainsKey(task)) { TaskName = InternalTaskNameLookup[task]; } InternalTaskIdentity++; var a = new XAttribute("await" + InternalTaskIdentity, TaskName); var s = this.selectorElement; var p = this; while (p != null) { if (p.selectorElement != null) { s = p.selectorElement; break; } p = this.parent; } a.AttachTo(s); var x = !this[a]; // when complete x.__task = task; // overkill Native.window.onframe += delegate { if (a == null) return; if (task.IsCompleted) { if (InternalTaskNameLookup.ContainsKey(task)) InternalTaskNameLookup.Remove(task); a.Remove(); a = null; } }; // how should we be introducing the conditional? document level? return x; } }
//[Variation(Priority = 1, Desc = "XElement - Change attribute's parent in the pre-event handler")] public void ParentedAttribute() { bool firstTime = true; XElement element = XElement.Parse("<root></root>"); XAttribute child = new XAttribute("Add", "Me"); element.Add(child); element.Changing += new EventHandler<XObjectChangeEventArgs>( delegate (object sender, XObjectChangeEventArgs e) { if (firstTime) { firstTime = false; child.Remove(); } }); try { child.Remove(); } catch (InvalidOperationException) { element.Verify(); return; } throw new TestFailedException("Should have thrown an InvalidOperationException"); }
public void MultiStringHasCorrectRepsonses() { const string str = @"<CmPossibilityList guid='cf379f73-9ee5-4e45-b2e2-4b169666d83e'> <Description> <AStr ws='en'> <Run ws='en'>English multi-string description.</Run> </AStr> <AStr ws='es'> <Run ws='es'>Spanish multi-string description.</Run> </AStr> </Description> </CmPossibilityList>"; var element = XElement.Parse(str); AddBasicPropertyElementsToPossList(element); var result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNull(result); var badAttr = new XAttribute("bogusAttr", "badvalue"); element.Element("Description").Add(badAttr); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has unrecognized attribute(s)")); badAttr.Remove(); var extraChild = new XElement("extraChild"); element.Element("Description").Add(extraChild); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has non-AStr child element")); extraChild.Remove(); // Test the <Run> element. var runElement = element.Element("Description").Element("AStr").Element("Run"); runElement.Add(extraChild); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Has non-text child element")); extraChild.Remove(); runElement.Add(badAttr); result = CmObjectValidator.ValidateObject(_mdc, element); Assert.IsNotNull(result); Assert.IsTrue(result.Contains("Invalid attribute for <Run> element")); badAttr.Remove(); }
//[Variation(Priority = 2, Desc = "Remove standalone attribute - def namespace", Params = new object[] { "xmlns", "value" })] //[Variation(Priority = 2, Desc = "Remove standalone attribute - namespace", Params = new object[] { "{http://www.w3.org/2000/xmlns/}p", "value" })] //[Variation(Priority = 2, Desc = "Remove standalone attribute I.", Params = new object[] { "{a}aa", "value" })] //[Variation(Priority = 2, Desc = "Remove standalone attribute II.", Params = new object[] { "aa", "value" })] public void RemoveStandaloneAttribute() { _runWithEvents = (bool)Params[0]; var name = (string)Variation.Params[0]; var value = (string)Variation.Params[1]; var a = new XAttribute(name, value); try { if (_runWithEvents) { _eHelper = new EventsHelper(a); } a.Remove(); if (_runWithEvents) { _eHelper.Verify(XObjectChange.Remove, a); } TestLog.Compare(false, "Exception was expected here"); } catch (InvalidOperationException) { // Expected exception } a.Verify(); }
private static void FixUpEntityTypeMapping( XAttribute storeEntitySetAttribute, XElement entityTypeMappingNode) { XName xn = XName.Get("MappingFragment", entityTypeMappingNode.Name.NamespaceName); XElement mf = new XElement(xn); // move the StoreEntitySet attribute into this node storeEntitySetAttribute.Remove(); mf.Add(storeEntitySetAttribute); // now move all descendants into this node ReparentChildren(entityTypeMappingNode, mf); entityTypeMappingNode.Add(mf); }