/// <summary> /// Serializes this Resource to XML with the passed in XmlWriter. /// </summary> /// <param name="writer">the writer to serialize the Resource to</param> /// <exception cref="ArgumentNullException">writer is null</exception> public void WriteXml(XmlWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } if (String.IsNullOrEmpty(writer.LookupPrefix(AnnotationXmlConstants.Namespaces.CoreSchemaNamespace))) { writer.WriteAttributeString(AnnotationXmlConstants.Prefixes.XmlnsPrefix, AnnotationXmlConstants.Prefixes.CoreSchemaPrefix, null, AnnotationXmlConstants.Namespaces.CoreSchemaNamespace); } writer.WriteAttributeString(AnnotationXmlConstants.Attributes.Id, XmlConvert.ToString(_id)); if (_name != null) { writer.WriteAttributeString(AnnotationXmlConstants.Attributes.ResourceName, _name); } // Use the actual field here to avoid creating the collection for no reason if (_locators != null) { foreach (ContentLocatorBase locator in _locators) { if (locator != null) { if (locator is ContentLocatorGroup) { LocatorGroupSerializer.Serialize(writer, locator); } else { ListSerializer.Serialize(writer, locator); } } } } // Use the actual field here to avoid creating the collection for no reason if (_contents != null) { foreach (XmlElement content in _contents) { if (content != null) { content.WriteTo(writer); } } } }
/// <summary> /// Deserializes an Resource from the XmlReader passed in. /// </summary> /// <param name="reader">reader to deserialize from</param> /// <exception cref="ArgumentNullException">reader is null</exception> public void ReadXml(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } XmlDocument doc = new XmlDocument(); ReadAttributes(reader); if (!reader.IsEmptyElement) { reader.Read(); // Reads the remainder of "Resource" start tag while (!(AnnotationXmlConstants.Elements.Resource == reader.LocalName && XmlNodeType.EndElement == reader.NodeType)) { if (AnnotationXmlConstants.Elements.ContentLocatorGroup == reader.LocalName) { ContentLocatorBase locator = (ContentLocatorBase)LocatorGroupSerializer.Deserialize(reader); InternalLocators.Add(locator); } else if (AnnotationXmlConstants.Elements.ContentLocator == reader.LocalName) { ContentLocatorBase locator = (ContentLocatorBase)ListSerializer.Deserialize(reader); InternalLocators.Add(locator); } else if (XmlNodeType.Element == reader.NodeType) { XmlElement element = doc.ReadNode(reader) as XmlElement; InternalContents.Add(element); } else { // The resource must contain a non-XmlElement child such as plain // text which is not part of the schema. throw new XmlException(SR.Get(SRID.InvalidXmlContent, AnnotationXmlConstants.Elements.Resource)); } } } reader.Read(); // Reads the end of the "Resource" element (or whole element if empty) }