//------------------------------------------------------ // // Private Methods // //------------------------------------------------------ #region Private Methods /// <summary> /// Reads all attributes for the "Resource" element and throws /// appropriate exceptions if any required attributes are missing /// or unexpected attributes are found /// </summary> private void ReadAttributes(XmlReader reader) { Invariant.Assert(reader != null, "No reader passed in."); // Use a temporary variable to determine if a Guid value // was actually provided. The member variable is set in // the default ctor and can't be relied on for this. Guid tempId = Guid.Empty; // Read all the attributes while (reader.MoveToNextAttribute()) { string value = reader.Value; // Skip null values - they will be treated the same as if // they weren't specified at all if (value == null) { continue; } switch (reader.LocalName) { case AnnotationXmlConstants.Attributes.Id: tempId = XmlConvert.ToGuid(value); break; case AnnotationXmlConstants.Attributes.ResourceName: _name = value; break; default: if (!Annotation.IsNamespaceDeclaration(reader)) { throw new XmlException(SR.Get(SRID.UnexpectedAttribute, reader.LocalName, AnnotationXmlConstants.Elements.Resource)); } break; } } if (Guid.Empty.Equals(tempId)) { throw new XmlException(SR.Get(SRID.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.Id, AnnotationXmlConstants.Elements.Resource)); } _id = tempId; // Name attribute is optional, so no need to check it // Move back to the parent "Resource" element reader.MoveToContent(); }
// Token: 0x06006273 RID: 25203 RVA: 0x001B9D9C File Offset: 0x001B7F9C internal static void CheckForNonNamespaceAttribute(XmlReader reader, string elementName) { Invariant.Assert(reader != null, "No reader supplied."); Invariant.Assert(elementName != null, "No element name supplied."); while (reader.MoveToNextAttribute()) { if (!Annotation.IsNamespaceDeclaration(reader)) { throw new XmlException(SR.Get("UnexpectedAttribute", new object[] { reader.LocalName, elementName })); } } reader.MoveToContent(); }
// Token: 0x060062AF RID: 25263 RVA: 0x001BB014 File Offset: 0x001B9214 private void ReadAttributes(XmlReader reader) { Invariant.Assert(reader != null, "No reader passed in."); Guid guid = Guid.Empty; while (reader.MoveToNextAttribute()) { string value = reader.Value; if (value != null) { string localName = reader.LocalName; if (!(localName == "Id")) { if (!(localName == "Name")) { if (!Annotation.IsNamespaceDeclaration(reader)) { throw new XmlException(SR.Get("UnexpectedAttribute", new object[] { reader.LocalName, "Resource" })); } } else { this._name = value; } } else { guid = XmlConvert.ToGuid(value); } } } if (Guid.Empty.Equals(guid)) { throw new XmlException(SR.Get("RequiredAttributeMissing", new object[] { "Id", "Resource" })); } this._id = guid; reader.MoveToContent(); }
/// <summary> /// Checks all attributes for the current node. If any attribute isn't a /// namespace attribute an exception is thrown. /// </summary> internal static void CheckForNonNamespaceAttribute(XmlReader reader, string elementName) { Invariant.Assert(reader != null, "No reader supplied."); Invariant.Assert(elementName != null, "No element name supplied."); while (reader.MoveToNextAttribute()) { // If the attribute is a namespace declaration we should ignore it if (Annotation.IsNamespaceDeclaration(reader)) { continue; } throw new XmlException(SR.Get(SRID.UnexpectedAttribute, reader.LocalName, elementName)); } // We need to move the reader back to the original element the // attributes are on for the next reader operation. Has no effect // if no attributes were looked at reader.MoveToContent(); }
/// <summary> /// Reads the internal data for this ContentLocator from the reader. This /// method is used by an XmlSerializer to deserialize a ContentLocator. Don't /// use this method directly, to deserialize a ContentLocator from Xml, use an /// XmlSerializer. /// </summary> /// <param name="reader">the reader to read internal data from</param> /// <exception cref="ArgumentNullException">reader is null</exception> public void ReadXml(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } // We expect no attributes on a "ContentLocator", // so throw using the name of one of the unexpected attributes Annotation.CheckForNonNamespaceAttribute(reader, AnnotationXmlConstants.Elements.ContentLocator); if (!reader.IsEmptyElement) { reader.Read(); // Reads the start of the "ContentLocator" element // ContentLocatorParts cannot write themselves out (see above). They could read // themselves in but instead of having write code in one place and read // code somewhere else - we keep it together in this class. while (!(AnnotationXmlConstants.Elements.ContentLocator == reader.LocalName && XmlNodeType.EndElement == reader.NodeType)) { if (XmlNodeType.Element != reader.NodeType) { throw new XmlException(SR.Get(SRID.InvalidXmlContent, AnnotationXmlConstants.Elements.ContentLocator)); } ContentLocatorPart part = new ContentLocatorPart(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI)); // Read each of the Item elements within the ContentLocatorPart if (!reader.IsEmptyElement) { // We expect no attributes on a locator part tag, // so throw using the name of one of the unexpected attributes Annotation.CheckForNonNamespaceAttribute(reader, part.PartType.Name); reader.Read(); // Read the start of the locator part tag while (!(XmlNodeType.EndElement == reader.NodeType && part.PartType.Name == reader.LocalName)) { if (AnnotationXmlConstants.Elements.Item == reader.LocalName && reader.NamespaceURI == AnnotationXmlConstants.Namespaces.CoreSchemaNamespace) { string name = null; string value = null; while (reader.MoveToNextAttribute()) { switch (reader.LocalName) { case AnnotationXmlConstants.Attributes.ItemName: name = reader.Value; break; case AnnotationXmlConstants.Attributes.ItemValue: value = reader.Value; break; default: if (!Annotation.IsNamespaceDeclaration(reader)) { throw new XmlException(SR.Get(SRID.UnexpectedAttribute, reader.LocalName, AnnotationXmlConstants.Elements.Item)); } break; } } if (name == null) { throw new XmlException(SR.Get(SRID.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.ItemName, AnnotationXmlConstants.Elements.Item)); } if (value == null) { throw new XmlException(SR.Get(SRID.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.ItemValue, AnnotationXmlConstants.Elements.Item)); } reader.MoveToContent(); part.NameValuePairs.Add(name, value); bool isEmpty = reader.IsEmptyElement; reader.Read(); // Read the beginning of the complete Item tag if (!isEmpty) { if (!(XmlNodeType.EndElement == reader.NodeType && AnnotationXmlConstants.Elements.Item == reader.LocalName)) { // Should not contain any content, only attributes throw new XmlException(SR.Get(SRID.InvalidXmlContent, AnnotationXmlConstants.Elements.Item)); } else { reader.Read(); // Read the end of the Item tag } } } else { // The locator part contains data other than just "Item" tags throw new XmlException(SR.Get(SRID.InvalidXmlContent, part.PartType.Name)); } } } _parts.Add(part); reader.Read(); // Read the ContentLocatorPart element } } reader.Read(); // Reads the end of the "ContentLocator" element (or whole element if empty) }
// Token: 0x06006275 RID: 25205 RVA: 0x001B9E24 File Offset: 0x001B8024 private void ReadAttributes(XmlReader reader) { Invariant.Assert(reader != null, "No reader passed in."); while (reader.MoveToNextAttribute()) { string value = reader.Value; if (!string.IsNullOrEmpty(value)) { string localName = reader.LocalName; if (!(localName == "Id")) { if (!(localName == "CreationTime")) { if (!(localName == "LastModificationTime")) { if (!(localName == "Type")) { if (!Annotation.IsNamespaceDeclaration(reader)) { throw new XmlException(SR.Get("UnexpectedAttribute", new object[] { reader.LocalName, "Annotation" })); } } else { string[] array = value.Split(Annotation._Colon); if (array.Length == 1) { array[0] = array[0].Trim(); if (string.IsNullOrEmpty(array[0])) { throw new FormatException(SR.Get("InvalidAttributeValue", new object[] { "Type" })); } this._typeName = new XmlQualifiedName(array[0]); } else { if (array.Length != 2) { throw new FormatException(SR.Get("InvalidAttributeValue", new object[] { "Type" })); } array[0] = array[0].Trim(); array[1] = array[1].Trim(); if (string.IsNullOrEmpty(array[0]) || string.IsNullOrEmpty(array[1])) { throw new FormatException(SR.Get("InvalidAttributeValue", new object[] { "Type" })); } this._typeName = new XmlQualifiedName(array[1], reader.LookupNamespace(array[0])); } } } else { this._modified = XmlConvert.ToDateTime(value); } } else { this._created = XmlConvert.ToDateTime(value); } } else { this._id = XmlConvert.ToGuid(value); } } } if (this._id.Equals(Guid.Empty)) { throw new XmlException(SR.Get("RequiredAttributeMissing", new object[] { "Id", "Annotation" })); } if (this._created.Equals(DateTime.MinValue)) { throw new XmlException(SR.Get("RequiredAttributeMissing", new object[] { "CreationTime", "Annotation" })); } if (this._modified.Equals(DateTime.MinValue)) { throw new XmlException(SR.Get("RequiredAttributeMissing", new object[] { "LastModificationTime", "Annotation" })); } if (this._typeName == null) { throw new XmlException(SR.Get("RequiredAttributeMissing", new object[] { "Type", "Annotation" })); } reader.MoveToContent(); }
//------------------------------------------------------ // // Private Methods // //------------------------------------------------------ #region Private Methods private void ReadAttributes(XmlReader reader) { Invariant.Assert(reader != null, "No reader passed in."); // Read all the attributes while (reader.MoveToNextAttribute()) { string value = reader.Value; // Skip null and empty values - they will be treated the // same as if they weren't specified at all if (String.IsNullOrEmpty(value)) { continue; } switch (reader.LocalName) { case AnnotationXmlConstants.Attributes.Id: _id = XmlConvert.ToGuid(value); break; // XmlConvert.ToDateTime is [Obsolete] #pragma warning disable 0618 case AnnotationXmlConstants.Attributes.CreationTime: _created = XmlConvert.ToDateTime(value); break; case AnnotationXmlConstants.Attributes.LastModificationTime: _modified = XmlConvert.ToDateTime(value); break; #pragma warning restore 0618 case AnnotationXmlConstants.Attributes.TypeName: string[] typeName = value.Split(_Colon); if (typeName.Length == 1) { typeName[0] = typeName[0].Trim(); if (String.IsNullOrEmpty(typeName[0])) { // Just a string of whitespace (empty string doesn't get processed) throw new FormatException(SR.Get(SRID.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName)); } _typeName = new XmlQualifiedName(typeName[0]); } else if (typeName.Length == 2) { typeName[0] = typeName[0].Trim(); typeName[1] = typeName[1].Trim(); if (String.IsNullOrEmpty(typeName[0]) || String.IsNullOrEmpty(typeName[1])) { // One colon, prefix or suffix is empty string or whitespace throw new FormatException(SR.Get(SRID.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName)); } _typeName = new XmlQualifiedName(typeName[1], reader.LookupNamespace(typeName[0])); } else { // More than one colon throw new FormatException(SR.Get(SRID.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName)); } break; default: if (!Annotation.IsNamespaceDeclaration(reader)) { throw new XmlException(SR.Get(SRID.UnexpectedAttribute, reader.LocalName, AnnotationXmlConstants.Elements.Annotation)); } break; } } // Test to see if any required attribute was missing if (_id.Equals(Guid.Empty)) { throw new XmlException(SR.Get(SRID.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.Id, AnnotationXmlConstants.Elements.Annotation)); } if (_created.Equals(DateTime.MinValue)) { throw new XmlException(SR.Get(SRID.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.CreationTime, AnnotationXmlConstants.Elements.Annotation)); } if (_modified.Equals(DateTime.MinValue)) { throw new XmlException(SR.Get(SRID.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.LastModificationTime, AnnotationXmlConstants.Elements.Annotation)); } if (_typeName == null) { throw new XmlException(SR.Get(SRID.RequiredAttributeMissing, AnnotationXmlConstants.Attributes.TypeName, AnnotationXmlConstants.Elements.Annotation)); } // Move back to the parent "Annotation" element reader.MoveToContent(); }
/// <summary>Deserializes the <see cref="T:System.Windows.Annotations.ContentLocator" /> from a specified <see cref="T:System.Xml.XmlReader" />.</summary> /// <param name="reader">The XML reader to use to deserialize the <see cref="T:System.Windows.Annotations.ContentLocator" />.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="reader" /> is <see langword="null" />.</exception> /// <exception cref="T:System.Xml.XmlException">The serialized XML for the <see cref="T:System.Windows.Annotations.ContentLocator" /> is not valid.</exception> // Token: 0x0600633B RID: 25403 RVA: 0x001BE740 File Offset: 0x001BC940 public void ReadXml(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } Annotation.CheckForNonNamespaceAttribute(reader, "ContentLocator"); if (!reader.IsEmptyElement) { reader.Read(); while (!("ContentLocator" == reader.LocalName) || XmlNodeType.EndElement != reader.NodeType) { if (XmlNodeType.Element != reader.NodeType) { throw new XmlException(SR.Get("InvalidXmlContent", new object[] { "ContentLocator" })); } ContentLocatorPart contentLocatorPart = new ContentLocatorPart(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI)); if (!reader.IsEmptyElement) { Annotation.CheckForNonNamespaceAttribute(reader, contentLocatorPart.PartType.Name); reader.Read(); while (XmlNodeType.EndElement != reader.NodeType || !(contentLocatorPart.PartType.Name == reader.LocalName)) { if (!("Item" == reader.LocalName) || !(reader.NamespaceURI == "http://schemas.microsoft.com/windows/annotations/2003/11/core")) { throw new XmlException(SR.Get("InvalidXmlContent", new object[] { contentLocatorPart.PartType.Name })); } string text = null; string text2 = null; while (reader.MoveToNextAttribute()) { string localName = reader.LocalName; if (!(localName == "Name")) { if (!(localName == "Value")) { if (!Annotation.IsNamespaceDeclaration(reader)) { throw new XmlException(SR.Get("UnexpectedAttribute", new object[] { reader.LocalName, "Item" })); } } else { text2 = reader.Value; } } else { text = reader.Value; } } if (text == null) { throw new XmlException(SR.Get("RequiredAttributeMissing", new object[] { "Name", "Item" })); } if (text2 == null) { throw new XmlException(SR.Get("RequiredAttributeMissing", new object[] { "Value", "Item" })); } reader.MoveToContent(); contentLocatorPart.NameValuePairs.Add(text, text2); bool isEmptyElement = reader.IsEmptyElement; reader.Read(); if (!isEmptyElement) { if (XmlNodeType.EndElement != reader.NodeType || !("Item" == reader.LocalName)) { throw new XmlException(SR.Get("InvalidXmlContent", new object[] { "Item" })); } reader.Read(); } } } this._parts.Add(contentLocatorPart); reader.Read(); } } reader.Read(); }