【发布时间】:2014-07-12 17:50:37
【问题描述】:
我想解析 xml 模式 (xsd) 中的所有信息/元素,并将它们作为我的程序中的变量使用。我从微软找到这篇文章:http://msdn.microsoft.com/en-us/library/ms255932.aspx
来自 Microsoft 的代码示例
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>
程序运行没有错误,但我只得到第一棵树中的元素。示例:
输出:
Element: Version
Element: File
但我想要所有元素:Min, Max from Version 和 Min, Max from File like:
我需要什么:
Element: Version
Element: Min
Element: Max
Element: File
Element: Min
Element: Max
所以我的问题:
- 如何获取所有元素?
- 我可以在我的程序中使用以下元素吗:Version.Min = 5.4
【问题讨论】:
-
不看代码就很难诊断出你的代码在做什么或不做什么!
-
对不起,我会更改它,但我使用的是微软文章中的代码示例
标签: c# .net xml xml-parsing xsd