c# - 遍历 XML 架构失败。 (使用 System.Xml.Schema 和 C#)

标签 c# xml xsd

美好的一天。

当我尝试运行 MSDN 中的示例时。但在运行时失败了。

错误警报:

System.Xml.dll 中发生“System.Xml.XmlException”类型的未处理异常

附加信息:“type”是一个意外的标记。期待空白。第 7 行,位置 24。

我的代码 http://msdn.microsoft.com/en-us/library/ms255932.aspx

using System; 
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
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", "address.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);
    }
} 

`

最佳答案

您的 xsd 文件格式不正确。

<xs:element name="city"type="xs:string"/>
<xs:element name="country"type="xs:string"/>

类型前需要空格。

<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>

关于c# - 遍历 XML 架构失败。 (使用 System.Xml.Schema 和 C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1872943/

相关文章:

c# - GetPartitions 得到了偏斜的分区?

c# - 如何继续第一个成功的任务,如果所有任务都失败则抛出异常

.net - 解析 XML 的不同方法

java - Simplexml 列表不起作用

c# - 获取子元素的所有属性

c# - 在 C# 中让线程从主线程获取变量值的问题

c# - 我应该如何从 Entity Framework 4.1 中的存储过程返回一个 int?

java - 在eclipse中编辑xml时如何让Ctrl-F11构建项目?

xml - XSD 嵌套元素

wcf - 如何将复杂类型嵌入到 wsdl 定义中?