http://dotnet.aspx.cc/article/7b4c7a42-4cdf-40d1-b293-e86da109a34c/read.aspx跟普通的XML文档的区别就是他是带命名空间的

解决方案 »

  1.   

    //读写 XML 架构
    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Schema;class XmlSchemaReadWriteExample
    {
        static void Main()
        {
            try
            {
                XmlTextReader reader = new XmlTextReader("example.xsd");
                XmlSchema myschema = XmlSchema.Read(reader, ValidationCallback);
                myschema.Write(Console.Out);
                FileStream file = new FileStream("new.xsd", FileMode.Create, FileAccess.ReadWrite);
                XmlTextWriter xwriter = new XmlTextWriter(file, new UTF8Encoding());
                xwriter.Formatting = Formatting.Indented;
                myschema.Write(xwriter);
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }    static void ValidationCallback(object sender, ValidationEventArgs args)
        {
            if (args.Severity == XmlSeverityType.Warning)
                Console.Write("WARNING: ");
            else if (args.Severity == XmlSeverityType.Error)
                Console.Write("ERROR: ");        Console.WriteLine(args.Message);
        }
    }//遍历 XML 架构
    using System;
    using System.Collections;
    using System.Xml;
    using System.Xml.Schema;class XmlSchemaTraverseExample
    {
        static void Main()
        {
            // Add the customer schema to a new XmlSchemaSet and compile it.
            // Any schema validation warnings and errors encountered reading or 
            // compiling the schema are handled by the ValidationEventHandler delegate.
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
            schemaSet.Add("http://www.tempuri.org", "customer.xsd");
            schemaSet.Compile();        // Retrieve the compiled XmlSchema object from the XmlSchemaSet
            // by iterating over the Schemas property.
            XmlSchema customerSchema = null;
            foreach (XmlSchema schema in schemaSet.Schemas())
            {
                customerSchema = schema;
            }        // Iterate over each XmlSchemaElement in the Values collection
            // of the Elements property.
            foreach (XmlSchemaElement element in customerSchema.Elements.Values)
            {            Console.WriteLine("Element: {0}", element.Name);            // Get the complex type of the Customer element.
                XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType;            // If the complex type has any attributes, get an enumerator 
                // and write each attribute name to the console.
                if (complexType.AttributeUses.Count > 0)
                {
                    IDictionaryEnumerator enumerator =
                        complexType.AttributeUses.GetEnumerator();                while (enumerator.MoveNext())
                    {
                        XmlSchemaAttribute attribute =
                            (XmlSchemaAttribute)enumerator.Value;                    Console.WriteLine("Attribute: {0}", attribute.Name);
                    }
                }            // Get the sequence particle of the complex type.
                XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;            // Iterate over each XmlSchemaElement in the Items collection.
                foreach (XmlSchemaElement childElement in sequence.Items)
                {
                    Console.WriteLine("Element: {0}", childElement.Name);
                }
            }
        }    static void ValidationCallback(object sender, ValidationEventArgs args)
        {
            if (args.Severity == XmlSeverityType.Warning)
                Console.Write("WARNING: ");
            else if (args.Severity == XmlSeverityType.Error)
                Console.Write("ERROR: ");        Console.WriteLine(args.Message);
        }
    }
      

  2.   

    XmlSchemaSet   在2.0里才有
    在前面的版本里是没有的,我用的VS2003,net Framework版本非2.0
      

  3.   

    直接用XMLREADER或是XMLDOCUMENT解决不了吗?
      

  4.   

    下面的应该2003支持的
    //生成XmlSchema
    private const String document = "sample.xsd";
    XmlSchema mySchema = XmlSchema.Read(new XmlTextReader(document));myXmlTextWriter = new XmlTextWriter(Console.Out);
    myXmlTextWriter.Formatting = Formatting.Indented;
    myXmlTextWriter.Indentation = 2;// Write out the XSD
        void WriteXSDSchema()
        {
            myXmlTextWriter.WriteStartElement("schema", XmlSchema.Namespace);
            myXmlTextWriter.WriteAttributeString("targetNamespace", myXmlSchema.TargetNamespace);
            foreach(XmlSchemaInclude include in myXmlSchema.Includes)
            {
                myXmlTextWriter.WriteStartElement("include", XmlSchema.Namespace);
                myXmlTextWriter.WriteAttributeString("schemaLocation", include.SchemaLocation);
                myXmlTextWriter.WriteEndElement();
            }        foreach(object item in myXmlSchema.Items)
            {
                if (item is XmlSchemaAttribute)
                    WriteXmlSchemaAttribute((XmlSchemaAttribute)item);      //attribute
                else if (item is XmlSchemaComplexType)
                    WriteXmlSchemaComplexType((XmlSchemaComplexType)item);  //complexType
                else if (item is XmlSchemaSimpleType)
                    WriteXmlSchemaSimpleType((XmlSchemaSimpleType)item);    //simpleType
                else if (item is XmlSchemaElement)
                    WriteXmlSchemaElement((XmlSchemaElement)item);          //element
                else if (item is XmlSchemaAnnotation)
                    WriteXmlSchemaAnnotation((XmlSchemaAnnotation)item);    //annotation
                else if (item is XmlSchemaAttributeGroup)
                    WriteXmlSchemaAttributeGroup((XmlSchemaAttributeGroup)item); //attributeGroup
                else if (item is XmlSchemaNotation)
                    WriteXmlSchemaNotation((XmlSchemaNotation)item);        //notation
                else if (item is XmlSchemaGroup)
                    WriteXmlSchemaGroup((XmlSchemaGroup)item);              //group
                else
                    Console.WriteLine("Not Implemented.");
            }
            myXmlTextWriter.WriteEndElement();
        }