开发者

Is it possible to serialize an array so that its elements aren't wrapped in a tag for the array?

I have the following Rule and Ruleset classes:

[XmlInclude(typeof(OrRule))]
[XmlInclude(typeof(AndRule))]
[XmlInclude(typeof(EmptyRule))]
[XmlInclude(typeof(MatchRule))]
public class Rule {}

[XmlType("Or")]
public class OrRule
{
    public Rule[] Operands { get; set; }
}

[XmlType("And")]
public class AndRule
{
    public Rule[] Operands { get; set; }
}

[XmlType("Empty")]
public class EmptyRule {}

[XmlType("Match")]
public class MatchRule
{
    public string Regex { get; set; }
}

public class Ruleset
{
    public string Name { get; set; }

    [XmlArrayElement(typeof(OrRule))]
    [XmlArrayElement(typeof(AndRule))]
    [XmlArrayElement(typeof(EmptyRule))]
    [XmlArrayElement(typeof(MatchRule))]
    public Rule[] Rules { get; set; }
}

(I hope I got everything right - this is a radically simplified example and not the actual code.) These get serialized to something like this:

<Ruleset Name="PasswordEmptyAllowed">
  <Rules>
    <Or>
      <Operands>
        <Empty />
        <And>
          <Operands>
            <Match Regex="\d" />
            <Match Regex="[a-záéíóöőúüű]" />              
            <Match Regex="[A-ZÁÉÍÓÖŐÚÜŰ]" />
          </Operands>
        </And>
      </Operands>
    </Or>
  </Rules>
</Ruleset>

The extra <Rules> and <Operands> tags 开发者_StackOverflow社区are pretty ugly in my opinion, and they hurt readability. Is there a way to elimiate them?

Like this:

<Ruleset Name="PasswordEmptyAllowed">
  <Or>
    <Empty />
    <And>
      <Match Regex="\d" />
      <Match Regex="[a-záéíóöőúüű]" />              
      <Match Regex="[A-ZÁÉÍÓÖŐÚÜŰ]" />
    </And>
  </Or>
</Ruleset>


Try this:

[XmlElement("Rule")]
public Rule[] Rules {


I think you would have to manipulate the XML your self before/after it's converted to Object/XML.

But think about what you are trying to do.

I would like to disagree with you on tags being ugly. I think they help in readability, they keep child tags together.

imagine a tag with two such collections without a wrapping tag. With your approach, they can easily be mixed up together making them harder to read and serialize/deserialize.

Example:

<And>
  <Match Regex="\d" />
  <Replace From="123" To="ABC" />
  <Match Regex="[a-záéíóöőúüű]" />              
  <Replace From="_" To=" " />
  <Match Regex="[A-ZÁÉÍÓÖŐÚÜŰ]" />
  <Replace From="-" To="#" />
</And>

Instead of

<And>
  <Matches>
      <Match Regex="\d" />
      <Match Regex="[a-záéíóöőúüű]" />              
      <Match Regex="[A-ZÁÉÍÓÖŐÚÜŰ]" />
  </Matches>
  <Replaces>
      <Replace From="123" To="ABC" />      
      <Replace From="_" To=" " />
      <Replace From="-" To="#" />
  </Replaces>
</And>

You decide which one is readable and maintainable.

Think about what can go wrong with the approach you suggest and why XmlSerializer in .Net Framework promotes the current implementation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜