C#Parse的XML Schema的“ref”属性

时间:2009-12-10 01:17:04

标签: c# xml

美好的一天。

我的XSD文件的“ref”属性出了问题。 我的代码:

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.w3.org/2001/XMLSchema", "recipe.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}, {1}, {2}", childElement.RefName, childElement.MinOccurs, childElement.MaxOccurs);
            }
        }
    }

    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);
    }
}

我的XSD文件

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Recipe">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="DocumentInfo" minOccurs="1" maxOccurs="1" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <!-- Element of DocumentInfo -->
  <xsd:element name="DocumentInfo">
    <xsd:complexType>
      <xsd:attribute name="Name" type="xsd:string" />
      <xsd:attribute name="Description" type="xsd:string" />
      <xsd:attribute name="Creator" type="xsd:string" />
      <xsd:attribute name="CreateTime" type="xsd:string" />
      <xsd:attribute name="Revisor" type="xsd:string" />
      <xsd:attribute name="ReviseTime" type="xsd:string" />
      <xsd:attribute name="Version" type="xsd:string" />
      <xsd:attribute name="Frozen" type="xsd:boolean" />
      <xsd:attribute name="ASCSVersion" type="xsd:string" use="optional"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

现在,当我得到以下输出时:

Element: Recipe
Element: http://www.w3.org/2001/XMLSchema:DocumentInfo, 1, 1
Element: http://www.w3.org/2001/XMLSchema:Prerequisite, 1, 1
Element: http://www.w3.org/2001/XMLSchema:Headers, 0, 1
Element: http://www.w3.org/2001/XMLSchema:Steps, 1, 1

如何删除“http://www.w3.org/2001/XMLSchema”的前缀? 我只能使用“childElement.RefName”的属性,我找不到“childElement.Ref”。

DEV IDE:VS2005。 .NET 2.0。

在此先感谢。

BR! 纳米

1 个答案:

答案 0 :(得分:2)

您可以使用childElement.NamechildElement.QualifiedName.Name

XmlSchemaElement.Name

XmlSchemaElement.QualifiedName