Schema Reference Util : XPath « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » XML » XPathScreenshots 
Schema Reference Util
        
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace Open.MOF.SchemaReferenceUtil
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("USAGE SchemaReferenceUtil path");
                return;
            }

            string pathToFiles = args[0];
            string[] filenames = Directory.GetFiles(pathToFiles, "*.xsd");
            Dictionary<XmlDocument, string> schemaDocuments = new Dictionary<XmlDocument,string>();
            Dictionary<string, string> schemaLookup = new Dictionary<string,string>();
            Dictionary<string, string> schemaAdd = new Dictionary<string, string>();
            foreach (string filename in filenames)
            {
                FileInfo fileinfo = new FileInfo(filename);
                XmlDocument doc = new XmlDocument();
                schemaDocuments.Add(doc, filename);
                doc.Load(filename);
                XmlNode docElement = doc.DocumentElement;
                XmlAttribute tnsAttribute = docElement.Attributes["xmlns:tns"];
                if (tnsAttribute != null)
                {
                    string schemaName = tnsAttribute.Value;
                    if (!schemaLookup.ContainsKey(schemaName))
                        schemaLookup.Add(schemaName, fileinfo.Name);
                    else
                        schemaAdd.Add(schemaName, fileinfo.Name);
                }
            }

            foreach (XmlDocument doc in schemaDocuments.Keys)
            {
                bool wasSchemaChanged = false;
                XmlNode docElement = doc.DocumentElement;
                foreach (XmlNode node in docElement.ChildNodes)
                {
                    if (node.Name == "xs:import")
                    {
                        string schemaName = node.Attributes["namespace"].Value;
                        if (node.Attributes["schemaLocation"!= null)
                        {
                            node.Attributes["schemaLocation"].Value = schemaLookup[schemaName];
                        }
                        else
                        {
                            XmlAttribute schemaLocationAttribute = doc.CreateAttribute("schemaLocation");
                            schemaLocationAttribute.Value = schemaLookup[schemaName];
                            node.Attributes.Append(schemaLocationAttribute);
                        }

                        wasSchemaChanged = true;
                    }
                }

                if (wasSchemaChanged)
                    doc.Save(schemaDocuments[doc]);
            }

            schemaDocuments.Clear();
        }
    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.XPathNavigator
2.XPathNodeIterator
3.Select by path
4.Find Elements with an XPath SearchFind Elements with an XPath Search
5.XPath Query Demo
6.Read XML node using the XML path
7.XmlQuery Example
8. XPath Expression Syntax
9.Using XPath to get string value, integer value and boolean value
10.Extensions.XPathSelectElement selects an XElement using a XPath expression
11.Extensions.XPathSelectElements Selects a collection of elements using an XPath expression.
12.Returns the string result from evaluating an xpath expression against the given document and context.
13.Gets the text value from the element located by the given XPath.
14.Returns the inner text of the single node selected from the specified xpath if found; otherwise, null.
15.Get an array of nodes matching an XPath expression
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.