static void XPathNavigatorMethods_CreateAttributes() { // XPathNavigator.CreateAttributes() //<snippet8> XmlDocument document = new XmlDocument(); document.Load("contosoBooks.xml"); XPathNavigator navigator = document.CreateNavigator(); navigator.MoveToChild("bookstore", "http://www.contoso.com/books"); navigator.MoveToChild("book", "http://www.contoso.com/books"); navigator.MoveToChild("price", "http://www.contoso.com/books"); XmlWriter attributes = navigator.CreateAttributes(); attributes.WriteAttributeString("discount", "1.00"); attributes.WriteAttributeString("currency", "USD"); attributes.Close(); navigator.MoveToParent(); Console.WriteLine(navigator.OuterXml); //</snippet8> }
// the actual work of the component public override void Apply(XmlDocument document, string key) { // set the key in the XPath context context["key"] = key; // perform each copy action foreach (CopyCommand copy_command in copy_commands) { // get the source comment XPathExpression key_expression = copy_command.Key.Clone(); key_expression.SetContext(context); // Console.WriteLine(key_expression.Expression); string key_value = (string)document.CreateNavigator().Evaluate(key_expression); // Console.WriteLine("got key '{0}'", key_value); XPathNavigator data = copy_command.Index.GetContent(key_value); if (data == null && copy_command.IgnoreCase == "true") { data = copy_command.Index.GetContent(key_value.ToLower()); } // notify if no entry if (data == null) { WriteMessage(copy_command.MissingEntry, String.Format("No index entry found for key '{0}'.", key_value)); continue; } // get the target node String target_xpath = copy_command.Target.Clone().ToString(); XPathExpression target_expression = XPathExpression.Compile(string.Format(target_xpath, key_value)); target_expression.SetContext(context); XPathNavigator target = document.CreateNavigator().SelectSingleNode(target_expression); // notify if no target found if (target == null) { WriteMessage(copy_command.MissingTarget, String.Format("Target node '{0}' not found.", target_expression.Expression)); continue; } // get the source nodes XPathExpression source_expression = copy_command.Source.Clone(); source_expression.SetContext(context); XPathNodeIterator sources = data.CreateNavigator().Select(source_expression); // append the source nodes to the target node int source_count = 0; foreach (XPathNavigator source in sources) { source_count++; // If attribute=true, add the source attributes to current target. // Otherwise append source as a child element to target if (copy_command.Attribute == "true" && source.HasAttributes) { string source_name = source.LocalName; XmlWriter attributes = target.CreateAttributes(); source.MoveToFirstAttribute(); string attrFirst = target.GetAttribute(string.Format("{0}_{1}", source_name, source.Name), string.Empty); if (string.IsNullOrEmpty(attrFirst)) { attributes.WriteAttributeString(string.Format("{0}_{1}", source_name, source.Name), source.Value); } while (source.MoveToNextAttribute()) { string attrNext = target.GetAttribute(string.Format("{0}_{1}", source_name, source.Name), string.Empty); if (string.IsNullOrEmpty(attrNext)) { attributes.WriteAttributeString(string.Format("{0}_{1}", source_name, source.Name), source.Value); } } attributes.Close(); } else { target.AppendChild(source); } } // notify if no source found if (source_count == 0) { WriteMessage(copy_command.MissingSource, String.Format("Source node '{0}' not found.", source_expression.Expression)); } foreach (CopyComponent component in components) { component.Apply(document, key); } } }
//===================================================================== /// <inheritdoc /> public override void Apply(XmlDocument targetDocument, IXmlNamespaceResolver context) { // Get the index entry content XPathExpression keyExpr = this.Key.Clone(); keyExpr.SetContext(context); string keyValue = (string)targetDocument.CreateNavigator().Evaluate(keyExpr); XPathNavigator data = this.SourceIndex[keyValue]; if (data == null && !String.IsNullOrWhiteSpace(keyValue)) { if (this.IgnoreCase) { data = this.SourceIndex[keyValue.ToLowerInvariant()]; } if (data == null) { // For certain inherited explicitly implemented interface members, Microsoft is using // incorrect member IDs in the XML comments files and on the content service. If the // failed member ID looks like one of them, try using the broken ID for the look up. string brokenId = keyValue; if (reDictionaryEII.IsMatch(brokenId)) { brokenId = reGenericEII.Replace(brokenId, "#I$1{TKey@TValue}#"); } else if (reGenericEII.IsMatch(brokenId)) { brokenId = reGenericEII.Replace(brokenId, "#I$1{T}#"); } // Don't bother if they are the same if (brokenId != keyValue) { data = this.SourceIndex[brokenId]; } } } // Notify if no entry if (data == null) { base.ParentComponent.WriteMessage(this.MissingEntry, "No index entry found for key '{0}'.", keyValue); return; } // Get the target node XPathExpression targetExpr = this.Target.Clone(); targetExpr.SetContext(context); XPathNavigator target = targetDocument.CreateNavigator().SelectSingleNode(targetExpr); // notify if no target found if (target == null) { base.ParentComponent.WriteMessage(this.MissingTarget, "Target node '{0}' not found.", targetExpr.Expression); return; } // get the source nodes XPathExpression sourceExpr = this.Source.Clone(); sourceExpr.SetContext(context); XPathNodeIterator sources = data.CreateNavigator().Select(sourceExpr); // Append the source nodes to the target node foreach (XPathNavigator source in sources) { // If IsAttribute is true, add the source attributes to current target. Otherwise append source // as a child element to target. if (this.IsAttribute && source.HasAttributes) { string sourceName = source.LocalName; XmlWriter attributes = target.CreateAttributes(); source.MoveToFirstAttribute(); do { string attrValue = target.GetAttribute(String.Format(CultureInfo.InvariantCulture, "{0}_{1}", sourceName, source.Name), String.Empty); if (string.IsNullOrEmpty(attrValue)) { attributes.WriteAttributeString(String.Format(CultureInfo.InvariantCulture, "{0}_{1}", sourceName, source.Name), source.Value); } } while(source.MoveToNextAttribute()); attributes.Close(); } else { target.AppendChild(source); } } // Notify if no source found if (sources.Count == 0) { base.ParentComponent.WriteMessage(this.MissingSource, "Source node '{0}' not found.", sourceExpr.Expression); } }
// // PRIVATE METHODS // private void CreateReport() { // Exit if Workspace is NULL if (this._workspaceDocument == null) { return; } // DiagrammerEnvironment diagrammerEnvironment = DiagrammerEnvironment.Default; diagrammerEnvironment.OnProgressChanged(new ProgressEventArgs("Creating Report")); // Get Temporary File string filename = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N").ToUpper() + ".xml"); // Specific XML Settings XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.Encoding = Encoding.Default; writerSettings.Indent = false; writerSettings.NewLineHandling = NewLineHandling.Entitize; writerSettings.OmitXmlDeclaration = true; writerSettings.NewLineOnAttributes = false; // Create the XmlWriter object and write some content. this._writer = XmlWriter.Create(filename, writerSettings); // <DataReport> this._writer.WriteStartElement("XmlReport"); // Get ESRI Namespace XPathDocument document = new XPathDocument(this._workspaceDocument, XmlSpace.None); XPathNavigator navigator = document.CreateNavigator(); bool ok = navigator.MoveToFirstChild(); string esriNamespace = navigator.LookupNamespace("esri"); // Open Geodatabase Schema Document XmlDocument schemaDocument = new XmlDocument(); schemaDocument.LoadXml(Resources.FILE_GEODATABASE_EXCHANGE); // Add Namespaces to Schema Document XPathNavigator navigator2 = schemaDocument.CreateNavigator(); bool ok2 = navigator2.MoveToFirstChild(); XmlWriter writer = navigator2.CreateAttributes(); writer.WriteAttributeString("xmlns", esriNamespace); writer.WriteAttributeString("targetNamespace", esriNamespace); writer.Close(); // Load Schema Document StringReader stringReader = new StringReader(schemaDocument.OuterXml); XmlTextReader xmlTextReader = new XmlTextReader(stringReader); // Create XML Reader Settings XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.Schemas.Add(null, xmlTextReader); readerSettings.Schemas.Compile(); readerSettings.CheckCharacters = true; readerSettings.IgnoreComments = true; readerSettings.IgnoreWhitespace = true; readerSettings.ValidationType = ValidationType.Schema; readerSettings.XmlResolver = new XmlUrlResolver(); readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; readerSettings.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventHandler); // Load Source XML XmlTextReader txtreader = new XmlTextReader(this._workspaceDocument); // Validate Against XSL XmlReader reader = XmlReader.Create(txtreader, readerSettings); while (reader.Read()) { ; } // Close Readers xmlTextReader.Close(); stringReader.Close(); txtreader.Close(); reader.Close(); // </DataReport> this._writer.WriteEndElement(); // Close Writer this._writer.Close(); // Set Source XML this.Xml = filename; // Fire Invalidate Event so that the Report Tabbed Document can Reload this.OnInvalidated(new EventArgs()); // Clear Messages diagrammerEnvironment.OnProgressChanged(new ProgressEventArgs(string.Empty)); }