我有一个这样的xsd文件:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="transfer">
<xs:complexType>
<xs:sequence>
<xs:element name="sourceGLN" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="destinationGLN" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="actionType" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="P" /> <!-- Mal Alim (Purchase) -->
<xs:enumeration value="S" /> <!-- Satis (Sale) -->
<xs:enumeration value="C" /> <!-- Cancel Sale (Cancel) -->
<xs:enumeration value="R" /> <!-- Iade (Return) -->
<xs:enumeration value="D" /> <!-- Deaktivasyon (Deactivation) -->
<xs:enumeration value="M" /> <!-- Uretim (Manufacture) -->
<xs:enumeration value="I" /> <!-- Ithalat (Import) -->
<xs:enumeration value="X" /> <!-- Ihrac (eXport) -->
<xs:enumeration value="O" /> <!-- Sarf (cOnsume) -->
<xs:enumeration value="N" /> <!-- Bilgi (iNformation) -->
<xs:enumeration value="T" /> <!-- Devir (Transfer) -->
<xs:enumeration value="L" /> <!-- Devir Iptal (canceL Transfer) -->
<xs:enumeration value="F" /> <!-- Aktarim (non-its transFer) -->
<xs:enumeration value="K" /> <!-- Aktarim Iptal (non-its cancel transfer) -->
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="shipTo" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="documentNumber" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="documentDate" type="xs:date" minOccurs="0" maxOccurs="1" />
<xs:element name="note" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="version" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="carrier" type="carrierType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="carrierType">
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:element name="productList" type="productListType" minOccurs="1" maxOccurs="1" />
<xs:element name="carrier" type="carrierType" minOccurs="1" maxOccurs="1" />
</xs:choice>
</xs:sequence>
<xs:attribute name="carrierLabel" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="20" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="containerType" type="xs:string" use="optional" />
</xs:complexType>
<xs:complexType name="productListType">
<xs:sequence>
<xs:element name="serialNumber" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="GTIN" type="xs:string" use="required" />
<xs:attribute name="lotNumber" type="xs:string" use="required" />
<xs:attribute name="productionDate" type="xs:date" use="optional" />
<xs:attribute name="expirationDate" type="xs:date" use="required" />
<xs:attribute name="PONumber" type="xs:string" use="optional" />
</xs:complexType>
</xs:schema>
我有这样的代码:
using System;
using System.Collections;
using System.Xml;
using System.Xml.Schema;
namespace ConsoleApplication1
{
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", "C:\\Users\\ahmet.ulusoy\\Desktop\\pts_xml_schema_v_1_4.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);
}
}
}
当我尝试运行它时,它给了我第一个元素的元素名称。在那之后,我如何获取其他两个 xs:complexType name="carrierType" 和 xs:complexType name="productListType" 及其子元素和属性?
我做了这部分并会更新代码。
我还想用这些数据和数据类型创建一个通用类。我该怎么办?
谢谢你的提前。