的XmlSchema从XML文件推断 - 如何通过在XSD所有元素循环? [英] XmlSchema inferred from an XML file - how to iterate through all the elements in the XSD?

查看:126
本文介绍了的XmlSchema从XML文件推断 - 如何通过在XSD所有元素循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,我推断在运行时的XSD架构,使用 XmlSchemaInference 类。

I have an XML file and I'm inferring its XSD schema in run-time, using the XmlSchemaInference class.

示例文件:

<products>
    <product id="1" name="t-shirt">
        <size name="medium"/>
        <size name="large"/>
        <price>
            <net>10</net>
            <gross>25</gross>
        </price>
    </product>
    <product id="2" name="computer mouse">  
        <price>
            <net>50</net>       
        </price>
    </product>
</products>



它的工作 - 这很好地推断架构:

It does work - it infers the schema nicely:

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="products">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="product">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="unbounded" name="size">
                <xs:complexType>
                  <xs:attribute name="name" type="xs:string" use="required" />
                </xs:complexType>
              </xs:element>
              <xs:element name="price">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="net" type="xs:unsignedByte" />
                    <xs:element minOccurs="0" name="gross" type="xs:unsignedByte" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="id" type="xs:unsignedByte" use="required" />
            <xs:attribute name="name" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>



现在的问题是:

我如何可以遍历(递归?)通过从这个模式中的所有元素?他们是如何存储在由的XmlSchemaSet 类?我需要将它们呈现给用户,使他们能够做一些映射。

How can I iterate (recursively?) through all the elements from this schema? How are they stored by the XmlSchemaSet class? I need to present them to the user so they can do some mapping.

我的检索的XmlSchema XmlSchemaSet.Schemas 属性,然后呢? XmlSchema.Elements 只包含一个项目(产品),我无法找到任何方式来看待了一下它的子元件。

I am retrieving an XmlSchema from XmlSchemaSet.Schemas property, and then what? XmlSchema.Elements only contains one item (products), and I can't find any way to look up what its subelements are.

推荐答案

好吧!没有答案,并没有太大的兴趣 - 我想通了我自己

Okay! No answer and not much interest - I figured it out on my own.

我使用的代码从这个MSDN文章中,我用Google搜索了:的遍历XML架构

I used code from this MSDN article I googled up: Traversing XML Schemas

和我根据我的上递归解决方案。

And I based my recursive solution on it.

void PrintSchema(string xmlFilePath)
{
    var schemaSet = new XmlSchemaInference().InferSchema(XmlReader.Create(xmlFilePath));
    foreach (XmlSchemaElement element in schemaSet
        .Schemas()
        .Cast<XmlSchema>()
        .SelectMany(s => s.Elements.Values.Cast<XmlSchemaElement>()))
    {
        Debug.WriteLine(element.Name + " (element)");
        IterateOverElement(element.Name, element);
    }
}

void IterateOverElement(string root, XmlSchemaElement element)
{
    var complexType = element.ElementSchemaType as XmlSchemaComplexType;
    if (complexType == null) 
    {
        return;
    }
    if (complexType.AttributeUses.Count > 0)
    {
        var enumerator = complexType.AttributeUses.GetEnumerator();
        while (enumerator.MoveNext())
        {
            var attribute = (XmlSchemaAttribute)enumerator.Value;
            Debug.WriteLine(root + "." + attribute.Name + " (attribute)");
        }
    }
    var sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
    if (sequence == null) 
    {
        return;
    }
    foreach (XmlSchemaElement childElement in sequence.Items)
    {
        root += String.Concat(".", childElement.Name);
        Debug.WriteLine(root + " (element)");
        // recursion
        IterateOverElement(root, childElement);
    }
}



的输出是:

products (element)
products.product (element)
products.product.id (attribute)
products.product.name (attribute)
products.product.size (element)
products.product.size.name (attribute)
products.product.price (element)
products.product.price.net (element)
products.product.price.gross (element)

我留给你来判断这个API怎么友好的,特别是考虑如何稀缺的是这些特定类的MSDN文档。任何意见或见解表示赞赏。

I leave to you to judge how friendly this API is, especially given how scarce is the MSDN documentation on these particular classes. Any comments or insights are appreciated.

这篇关于的XmlSchema从XML文件推断 - 如何通过在XSD所有元素循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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