简体   繁体   English

如何为具有任意层次结构的类建模?

[英]How Could I Model a Class With Arbitrary Hierarchy?

Consider the following XML: 考虑以下XML:

<elements>
    <element attribute="value1">
        <or>
            <subelement attribute="value2" />
            <subelement attribute="value3" />
        </or>
    </element>
    <element attribute="value">
        <and>
            <subelement attribute="value4" />
            <subelement attribute="value5" />
            <or>
                <subelement attribute="value6" />
                <subelement attribute="value7" />
            </or>
        </and>
    </element>
</elements>

Note that the following is also valid: 请注意,以下内容也有效:

<elements>
    <element attribute="value1">
        <subelement attribute="value2" />
    </element>
</elements>

And so is this: 这也是:

<elements>
    <element attribute="value1">
        <and>
            <or>
                <subelement attribute="value2" />
                <subelement attribute="value3" />
            </or>
            <or>
                <subelement attribute="value4" />
                <subelement attribute="value5" />
            </or>
        </and>
    </element>
</elements>

(The above example would equate to (value2 or value3) AND (value4 or value5) (以上示例等于(值2或值3)与(值4或值5)

I tried using xsd.exe to create a XSD file from the XML and then a .cs class but it was not sufficient for my needs as I need something that can have an arbitrary depth. 我尝试使用xsd.exe从XML创建一个XSD文件,然后再创建一个.cs类,但这不足以满足我的需要,因为我需要可以具有任意深度的东西。

How would I model this in C# as classes? 我如何在C#中将其建模为类? I'm thinking perhaps along the lines of some sort of fluent builder pattern? 我在考虑某种流利的构建者模式?

Thanks, 谢谢,

Richard 理查德

Ps Anyone who can come up with a way to validate the rules created by such a class structure gets a /hug :) 附言:任何想出一种方法来验证由此类结构创建的规则的人都会得到/ hug :)

The following schema definition correctly validates all 3 of your XML examples. 以下架构定义正确地验证了您的所有3个XML示例。 Unfortunately XSD.exe crashes with a stack overflow exception when it tries to turn it in to C# code. 不幸的是,当XSD.exe尝试将其转换为C#代码时,它崩溃并产生堆栈溢出异常。 However setting the xsd file as the schema inside visual studio makes the xml editor work correctly and will point out any validation rules that your xml violates. 但是,将xsd文件设置为Visual Studio中的架构会使xml编辑器正常工作,并且会指出xml违反的所有验证规则。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="schema"
    targetNamespace="http://tempuri.org/schema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/schema.xsd"
    xmlns:mstns="http://tempuri.org/schema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="elements">
    <xs:complexType>
      <xs:sequence>
        <xs:element name ="element" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:choice>
              <xs:element name="and" type="subLogic"/>
              <xs:element name="or" type="subLogic"/>
              <xs:element name="subelement" type ="subElement" />
            </xs:choice>
            <xs:attribute name="attribute"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="subLogic">
    <xs:choice minOccurs="1" maxOccurs="unbounded">
      <xs:element name="subelement" type="subElement"/>
      <xs:element name="and" type="subLogic"/>
      <xs:element name="or" type="subLogic"/>
    </xs:choice>
  </xs:complexType>

  <xs:complexType name="subElement">
    <xs:attribute name="attribute"/>
  </xs:complexType>
</xs:schema>

Here is a changed version of the xml to make visual studio validate it and enables intellisense. 这是XML的更改版本,以使Visual Studio对其进行验证并启用智能感知。 Just be shure you have your xsd file selected under the schemas selector in the properites for the xml file. 只需确保在xml文件的属性中的schemas选择器下选择了xsd文件即可。

<?xml version="1.0" encoding="utf-8" ?>
<elements 
    xmlns="http://tempuri.org/schema.xsd"
    xmlns:mstns="http://tempuri.org/schema.xsd">
  <element  attribute="value1">
    <and>
      <or>
        <subelement attribute="value2" />
        <subelement attribute="value3" />
      </or>
      <or>
        <subelement attribute="value4" />
        <subelement attribute="value5" />
      </or>
    </and>
  </element>
</elements>

Without knowing more of what you are representing or what the classes will need to do here is a basic layout that mimic's the XSD. 在不了解您要表示的内容或类需要做什么的情况下,它是模仿XSD的基本布局。 XML you generate from this class will likely not validate, but it is just a matter of adding the correct attributes to the classes and properties so everything shows up with the correct name, the layout is correct. 从此类生成的XML可能无法验证,但这只是向类和属性添加正确的属性的问题,因此所有内容均以正确的名称显示,布局正确。

class SubLogic
{
}

abstract class CollectionType : SubLogic
{
    public SubLogic[] Sub { get; set; }
}

class And : CollectionType
{
}

class Or : CollectionType
{
}

class SubElement : SubLogic
{
    public string Attribute { get; set; }
}

[XmlRoot("elements")]
class ElementCollection
{
    public Element[] Elements {Get;Set}
}

class Element
{
    public SubLogic Sub { Get; set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM