遍历 XML 模式失败 [英] Traversing XML Schemas failed
本文介绍了遍历 XML 模式失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想解析 xml 模式 (xsd) 中的所有信息/元素,并将它们作为我程序中的变量使用.我从微软找到了这篇文章:http://msdn.microsoft.com/en-我们/图书馆/ms255932.aspx
I want to parse all information/elements from a xml schema (xsd) and working with them as variables in my program. I found this article from microsoft: http://msdn.microsoft.com/en-us/library/ms255932.aspx
来自微软的代码示例
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.tempuri.org", "customer.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);
}
}
XML 架构:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="network" type="netType"/>
<xs:complexType name="netType">
<xs:sequence>
<xs:element name="Version">
<xs:complexType>
<xs:sequence>
<xs:element name="Min" type="xs:int"/>
<xs:element name="Max" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="File">
<xs:complexType>
<xs:sequence>
<xs:element name="Min" type="xs:int"/>
<xs:element name="Max" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NW">
<xs:complexType>
<xs:sequence>
<xs:element name="Ethernet">
<xs:complexType>
<xs:sequence>
<xs:element name="Cons">
<xs:simpleType>
<xs:list itemType="xs:int"/>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Mail"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
<小时>
程序运行没有错误,但我只得到第一棵树中的元素.示例:
The program runs without errors, but I get only the elements in the first tree. Example:
输出:
Element: Version
Element: File
但我想要所有元素:Min, Max from Version 和 Min, Max from File like:
But I want all elements: Min, Max from Version and Min, Max from File like:
我需要什么:
Element: Version
Element: Min
Element: Max
Element: File
Element: Min
Element: Max
所以我的问题:
- 如何获取所有元素?
- 我可以在我的程序中使用如下元素吗:Version.Min = 5.4
推荐答案
请在下方找到代码
static void Main(string[] args)
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add("http://tempuri.org/customer.xsd", "customer.xsd");
schemaSet.Compile();
XmlSchema xmlSchema = null;
foreach (XmlSchema schema in schemaSet.Schemas())
{
xmlSchema = schema;
}
// Following 2 lines can be removed, as there is no use and may cause errors
// DataSet myDS = new DataSet();
// myDS.ReadXmlSchema("customer.xsd");
foreach (object item in xmlSchema.Items)
{
XmlSchemaElement schemaElement = item as XmlSchemaElement;
XmlSchemaComplexType complexType = item as XmlSchemaComplexType;
if (schemaElement != null)
{
Console.Out.WriteLine("Schema Element: {0}", schemaElement.Name);
XmlSchemaType schemaType = schemaElement.SchemaType;
XmlSchemaComplexType schemaComplexType = schemaType as XmlSchemaComplexType;
if (schemaComplexType != null)
{
XmlSchemaParticle particle = schemaComplexType.Particle;
XmlSchemaSequence sequence = particle as XmlSchemaSequence;
if (sequence != null)
{
foreach (XmlSchemaElement childElement in sequence.Items)
{
Console.Out.WriteLine(" Element/Type: {0}:{1}", childElement.Name,
childElement.SchemaTypeName.Name);
}
}
if (schemaComplexType.AttributeUses.Count > 0)
{
IDictionaryEnumerator enumerator = schemaComplexType.AttributeUses.GetEnumerator();
while (enumerator.MoveNext())
{
XmlSchemaAttribute attribute = (XmlSchemaAttribute)enumerator.Value;
Console.Out.WriteLine(" Attribute/Type: {0}", attribute.Name);
}
}
}
}
else if (complexType != null)
{
Console.Out.WriteLine("Complex Type: {0}", complexType.Name);
OutputElements(complexType.Particle);
if (complexType.AttributeUses.Count > 0)
{
IDictionaryEnumerator enumerator = complexType.AttributeUses.GetEnumerator();
while (enumerator.MoveNext())
{
XmlSchemaAttribute attribute = (XmlSchemaAttribute)enumerator.Value;
Console.Out.WriteLine(" Attribute/Type: {0}", attribute.Name);
}
}
}
Console.Out.WriteLine();
}
Console.Out.WriteLine();
Console.In.ReadLine();
}
private static void OutputElements(XmlSchemaParticle particle)
{
XmlSchemaSequence sequence = particle as XmlSchemaSequence;
XmlSchemaChoice choice = particle as XmlSchemaChoice;
XmlSchemaAll all = particle as XmlSchemaAll;
if (sequence != null)
{
Console.Out.WriteLine(" Sequence");
for (int i = 0; i < sequence.Items.Count; i++)
{
XmlSchemaElement childElement = sequence.Items[i] as XmlSchemaElement;
XmlSchemaSequence innerSequence = sequence.Items[i] as XmlSchemaSequence;
XmlSchemaChoice innerChoice = sequence.Items[i] as XmlSchemaChoice;
XmlSchemaAll innerAll = sequence.Items[i] as XmlSchemaAll;
if (childElement != null)
{
Console.Out.WriteLine(" Element/Type: {0}:{1}", childElement.Name,
childElement.SchemaTypeName.Name);
}
else OutputElements(sequence.Items[i] as XmlSchemaParticle);
}
}
else if (choice != null)
{
Console.Out.WriteLine(" Choice");
for (int i = 0; i < choice.Items.Count; i++)
{
XmlSchemaElement childElement = choice.Items[i] as XmlSchemaElement;
XmlSchemaSequence innerSequence = choice.Items[i] as XmlSchemaSequence;
XmlSchemaChoice innerChoice = choice.Items[i] as XmlSchemaChoice;
XmlSchemaAll innerAll = choice.Items[i] as XmlSchemaAll;
if (childElement != null)
{
Console.Out.WriteLine(" Element/Type: {0}:{1}", childElement.Name,
childElement.SchemaTypeName.Name);
}
else OutputElements(choice.Items[i] as XmlSchemaParticle);
}
Console.Out.WriteLine();
}
else if (all != null)
{
Console.Out.WriteLine(" All");
for (int i = 0; i < all.Items.Count; i++)
{
XmlSchemaElement childElement = all.Items[i] as XmlSchemaElement;
XmlSchemaSequence innerSequence = all.Items[i] as XmlSchemaSequence;
XmlSchemaChoice innerChoice = all.Items[i] as XmlSchemaChoice;
XmlSchemaAll innerAll = all.Items[i] as XmlSchemaAll;
if (childElement != null)
{
Console.Out.WriteLine(" Element/Type: {0}:{1}", childElement.Name,
childElement.SchemaTypeName.Name);
}
else OutputElements(all.Items[i] as XmlSchemaParticle);
}
Console.Out.WriteLine();
}
}
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:
And xsd that I have used:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="customer"
targetNamespace="http://tempuri.org/customer.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/customer.xsd"
xmlns:mstns="http://tempuri.org/customer.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Version">
<xs:complexType>
<xs:sequence>
<xs:element name="Min" type="xs:int"/>
<xs:element name="Max" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="File">
<xs:complexType>
<xs:sequence>
<xs:element name="Min" type="xs:int"/>
<xs:element name="Max" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这篇关于遍历 XML 模式失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文