我想显示XSD的所有元素 [英] I want to display all the elements of XSD
本文介绍了我想显示XSD的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示XSD的所有元素,在我的代码中出了什么问题,它只显示了第一个元素
I want to display all the elements of XSD what is wrong in my code it just show first element
XmlSchema xmlschema = ReadAndCompileSchema(xsdFilePath);
foreach (XmlSchemaElement xmlschemaelement in xmlschema.Elements.Values)
{
ls.Add(xmlschemaelement.Name);
ProcessElement(xmlschemaelement,ls);//, mdList*/);
}
foreach (string l in ls)
{
textBox1.Text = l;
}
}
public List<string> ProcessElement(XmlSchemaElement elem, List<string> ls)
{
if (elem.ElementSchemaType is XmlSchemaComplexType)
{
ls.Add(elem.Name);
XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
//checking if the complextype element has attributes or any of its base types
//has attributes and add it to the attribute list
foreach (DictionaryEntry obj in ct.AttributeUses)
{
ls.Add((obj.Value as XmlSchemaElement).Name);
}
ProcessSchemaObject(ct.ContentTypeParticle, ls);
}
else if (elem.ElementSchemaType is XmlSchemaSimpleType)
{
ls.Add(elem.Name);
}
return ls;
}
public List<string> ProcessSchemaObject(XmlSchemaObject obj, List<string> ls)
{
//by calling ProcessElement
if (obj is XmlSchemaElement)
{
ProcessElement(obj as XmlSchemaElement, ls);
return ls;
}
return ls;
}
}
}
推荐答案
到目前为止,我只能看到一个错误.如果您解决了该问题,但仍不能解决问题,请返回报告.
I can see just one bug so far. If you fix it but it does not solve your problem, please report back.
foreach (string l in ls)
{
textBox1.Text = l;
}
假设ls
是正确计算的.您可能想在textBox1
中显示整个集合(List<string> ls
).相反,您只需更改Text
ls.Count
次,每次都替换以前的Text
值.
您需要改为执行以下操作:
Let''s assume ls
is calculated correctly. You probably want to show whole collection (List<string> ls
) in textBox1
. Instead, you just change the Text
ls.Count
times, each time replacing previous value of Text
.
You need to do something like that instead:
using StringBuilder = System.Text.StringBuilder;
//...
StringBuilder sb = new StringBuilder(); //empty
foreach (string line in ls) {
sb.Append(line);
sb.Append("\r\n");
}
textBox1.Text = sb.ToString();
我可以部分解决问题,但我遇到的其他问题位于代码的错误区域. 请在这里找到错误的地方
I could partially solve the problem but I am facing other problem located in Error region in my code .
Please if you could find out where is the bug here
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace UnderstandingSOM
{
class Program
{
public static ArrayList ls;// = new ArrayList();
static void Main(string[] args)
{
ls = new ArrayList();
TraverseSchema("book.xsd", ls);
Console.WriteLine("print ls");
#region Error Comments
/*
* I donot know why here the result from the arraylist show me
* books
* book
* system.xml.schema.xmlschemaelement; which is instead of element name author , title.. so on
* system.xml.schema.xmlschemaelement;
* system.xml.schema.xmlschemaelement;
* system.xml.schema.xmlschemaelement;
*/
#endregion
PrintVlaue(ls);
Console.ReadLine();
}
private static void PrintVlaue(IEnumerable myList)
{
foreach (Object obj in myList)
{
Console.WriteLine(obj);
}
}
private static XmlSchema ReadAndCompileSchema(string fileName)
{
XmlTextReader tr = new XmlTextReader(fileName, new NameTable());
// The Read method will throw errors encountered
// on parsing the schema
try
{
try
{
XmlSchema schema = XmlSchema.Read(tr, new ValidationEventHandler(ValidationSchema));
tr.Close();
XmlSchemaSet xset = new XmlSchemaSet();
xset.Add(schema);
xset.ValidationEventHandler += new ValidationEventHandler(ValidationSchema);
// The Compile method will throw errors encountered on compiling the schema
xset.Compile();
return schema;
}
catch (XmlException xmlEx)
{
// Let the user know that error happened during the validation.
Console.WriteLine("Directory not found: " + xmlEx.Message);
return null;
}
}
catch (DirectoryNotFoundException dirEx)
{
// Let the user know that the directory did not exist.
Console.WriteLine("Directory not found: " + dirEx.Message);
return null;
}
}
private static void ValidationSchema(object sender, ValidationEventArgs args)
{
Console.WriteLine("Exception Severity: " + args.Severity);
Console.WriteLine(args.Message);
}
private static void TraverseSchema(string xsfFilename, ArrayList ls)
{
XmlSchema custSchema = ReadAndCompileSchema(xsfFilename);
try
{
foreach (XmlSchemaElement elem in custSchema.Elements.Values)
{
ProcessElement(elem, ls);
}
}
catch (NullReferenceException nRefEx)
{
// Let the user know that you try to reference an object in your code that does not exist.
Console.WriteLine("Value not found: " + nRefEx.Message);
}
}
private static ArrayList ProcessElement(XmlSchemaElement elem, ArrayList ls)
{
//Determine the complex nodes on XTree
if (elem.ElementSchemaType is XmlSchemaComplexType)
{
Console.WriteLine("Object\"Complex Element\" : {0}", elem.Name);
ls.Add(elem.Name);
Console.WriteLine("test ls" + ls.Count);
}
else
{
//Determine the Basic nodes on XTree
Console.WriteLine("Basic Element : {0}", elem.Name);
ls.Add(elem);
Console.WriteLine("test ls for basic.. " + ls.Count);
}
if (elem.ElementSchemaType is XmlSchemaComplexType)
{
XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
ProcessSchemaObject(ct.ContentTypeParticle,ls);
}
return ls;
}
private static ArrayList ProcessSequence(XmlSchemaSequence sequence, ArrayList ls)
{
ProcessItemCollection(sequence.Items,ls);
return ls;
}
private static ArrayList ProcessChoice(XmlSchemaChoice choice, ArrayList ls)
{
ProcessItemCollection(choice.Items, ls);
return ls;
}
private static ArrayList ProcessAll(XmlSchemaAll All, ArrayList ls)
{
ProcessItemCollection(All.Items, ls);
return ls;
}
private static ArrayList ProcessExtension(XmlSchemaComplexContentExtension extension, ArrayList ls)
{
Console.WriteLine("Generalization Relationship : Extension");
return ls;
}
private static ArrayList ProcessItemCollection(XmlSchemaObjectCollection objs, ArrayList ls)
{
foreach (XmlSchemaObject obj in objs)
ProcessSchemaObject(obj, ls);
return ls;
}
private static ArrayList ProcessSchemaObject(XmlSchemaObject obj, ArrayList ls)
{
if (obj is XmlSchemaElement)
ProcessElement(obj as XmlSchemaElement, ls);
if (obj is XmlSchemaChoice)
ProcessChoice(obj as XmlSchemaChoice,ls);
if (obj is XmlSchemaSequence)
ProcessSequence(obj as XmlSchemaSequence, ls);
if (obj is XmlSchemaAll)
ProcessAll(obj as XmlSchemaAll, ls);
if (obj is XmlSchemaComplexContentExtension)
ProcessExtension(obj as XmlSchemaComplexContentExtension, ls);
return ls;
}
}
}
这篇关于我想显示XSD的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文