【问题标题】:The Attribute of "ref" for XML Schema For C# ParseC# Parse 的 XML Schema 的“ref”属性
【发布时间】:2015-02-11 06:36:42
【问题描述】:

美好的一天。

我的 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”。

开发 IDE:VS2005。 .NET 2.0。

在这里提前致谢。

BR! 纳米

【问题讨论】:

  • 如果您使用编写代码的语言标记您的问题,您将获得更多流量。
  • 我为我的问题插入了 C# 标签。谢谢保罗。

标签: c# xml


【解决方案1】:

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

XmlSchemaElement.Name

XmlSchemaElement.QualifiedName

【讨论】:

  • 你好 Zach,childElement.QualifiedName.Name 现在工作了。非常感谢。纳米。
猜你喜欢
  • 2020-07-19
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多