打印XSD的所有元素 [英] Print all elements of XSD

查看:107
本文介绍了打印XSD的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }

        foreach (XmlSchemaObject schemaObject in compiledSchema.Items)
        {            
            //Console.WriteLine(schemaObject.GetType());
            if (schemaObject.GetType() == typeof(XmlSchemaElement))
            {
                XmlSchemaElement elementType = (XmlSchemaElement)schemaObject;
                Console.WriteLine("{0}{1}", "xmlschemaelement:", elementType.Name);
            }
            if (schemaObject.GetType() == typeof(XmlSchemaSimpleType))
            {
                XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)schemaObject;
                Console.WriteLine("{0} {1}", simpleType.Name, simpleType.Datatype.ValueType);
            }
            if (schemaObject.GetType() == typeof(XmlSchemaComplexType))
            {
                XmlSchemaComplexType complexType = (XmlSchemaComplexType)schemaObject;
                Console.WriteLine("{0}{1}", "xmlschemacomplexType:", complexType.Name);//, complexType.Datatype.ValueType);
                //ProcessElement(schemaObject as XmlSchemaElement);
                ProcessSchemaObject(schemaObject);
            }
            if (schemaObject.GetType() == typeof(XmlSchemaSequence)) { Console.WriteLine("insequence"); }
        }
        Console.ReadLine();
        xtr.Close();
    }
 
    private static void ProcessElement(XmlSchemaElement elem)
    {
        //Determine the complex nodes on XTree
        if (elem.ElementSchemaType is XmlSchemaComplexType)
        {
            Console.WriteLine("Object\"Complex Element\" : {0}", elem.Name);
            //Console.WriteLine("test ls" + ls.Count);
            XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
            ProcessSchemaObject(ct.ContentTypeParticle);
        }
        else
        {
            //Determine the Basic nodes on XTree
            Console.WriteLine("Basic Element : {0}", elem.Name);
            //Console.WriteLine("test ls for basic.. " + ls.Count);
        }
    }
 
    private static void ProcessSchemaObject(XmlSchemaObject obj)
    {
        if (obj is XmlSchemaElement)
            ProcessElement(obj as XmlSchemaElement);
        if (obj is XmlSchemaComplexType)
        {
            //XmlSchemaElement se = obj as XmlSchemaElement;
            Console.WriteLine("Debug : {0}", obj.GetType());
            ProcessElement(obj as XmlSchemaElement);
        }
 }
}


我遇到此错误:
当您尝试引用代码中不存在的对象时,会发生NullReferenceException.例如,您可能尝试使用一个对象而不先使用New关键字,或者尝试使用其值设置为null的对象
在这部分代码中


I face this error:
A NullReferenceException occurs when you try to reference an object in your code that does not exist. For example, you may have tried to use an object without using the New keyword first, or tried to use an object whose value is set to null
in this part of code

if (elem.ElementSchemaType is XmlSchemaComplexType)
{
  Console.WriteLine("Object\"Complex Element\" : {0}", elem.Name);
  //Console.WriteLine("test ls" + ls.Count);
  XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
  ProcessSchemaObject(ct.ContentTypeParticle);
}


它不应该是一个空值,它是在开始解析复杂类型时发生的.


It should not be a null value, that happened when it starts to parse complextype.

推荐答案

您可以尝试以下操作:
1.设置调试器以在引发异常时中断.
2.使用调用堆栈在方法之间进行导航
3.检查变量

希望这会有所帮助

问候
Espen Harlinn
You can try the following:
1. Set up the debugger to break when the exception is thrown.
2. Use the callstack to navigate between the methods
3. inspect the variables

Hope this helps

Regards
Espen Harlinn


不能将XmlSchemaComplexType强制转换为 XmlSchemaElement.

An XmlSchemaComplexType cannot be cast as XmlSchemaElement.

if (obj is XmlSchemaComplexType)
        {
            //XmlSchemaElement se = obj as XmlSchemaElement;
            Console.WriteLine("Debug : {0}", obj.GetType());
            ProcessElement(obj as XmlSchemaElement);
        }


ProcessElement中的参数elem将是null.

干杯


Your parameter elem in ProcessElement will be null.

Cheers


这篇关于打印XSD的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆